vue中height从0到auto,子元素高度过渡动画

本文介绍如何使用iView框架实现按钮点击后的图标切换及内容区域的展开与收起动画效果。通过JavaScript控制DOM样式变化,实现了平滑过渡动画,并避免了因v-if或v-show引起的动画失效问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考文章
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';
      }
    },
  }, 

参考文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值