参考文章
1.template中,这里用的是iview框架的按钮和icon图标,根据点击按钮切换图标和展开收起动画
<qf-button
size="small"
type="text"
@click="setContent"
>
<qf-icon
:type="isActive ? 'angle-double-up' : 'angle-double-down'"
size="12"
color="#999"
class="mr-9"
/>
</qf-button>
<div ref="content">
<p>详细信息</p>
<p>子元素信息</p>
</div>
2.data,mounted,methods中
data() {
return {
isActive: false,
textheight: 0
};
},
mounted() {
this.$nextTick(el => {
// 不能通过v-if,v-show来控制元素显示隐藏,否则动画不生效,这里是默认隐藏
this.textheight = this.$refs.content.offsetHeight + "px";
this.$refs.content.style.height = 0;
this.$refs.content.style.overflow = 'hidden';
// -------------两种情况,如果默认隐藏选上面,默认展示选下面-------------
// 不能通过v-if,v-show来控制元素显示隐藏,否则动画不生效,
// 默认显示数据,就要先获取到子元素的高度。
this.textheight = `${this.$refs.content.offsetHeight}px`;
this.$refs.content.style.height = this.textheight;
});
},
methods: {
setContent() {
this.isActive = !this.isActive;
if (this.isActive) {
this.$refs.content.style.height = this.textheight;
this.$refs.content.style.opacity = 1;
} else {
this.$refs.content.style.height = 0;
this.$refs.content.style.opacity = 0;
this.$refs.content.style.overflow = 'hidden';
}
},
},