点击展开伸缩功能

文章展示了如何在Vue2项目中使用模板和CSS动画实现一个组件的收缩与展开效果,通过JavaScript控制元素的宽度变化和类名切换以触发动画。
摘要由CSDN通过智能技术生成

直接上代码vue2代码

<template>
  <div class="">
   
    <div class="box">

      <div class="left" :class="tag ? 'animationIn' : 'animationOut'">
        11
      </div>
      <div class="right">
        <div class="btn" @click="tag = !tag">
           <button >点击</button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: '',
  components: {},
  data() {
    // 这里存放数据
    return {
      tag: true
    }
  }
}
</script>
<style lang="scss" scoped>
.btn {
  
  /* 子元素设置绝对定位 */
  position: absolute;
  top: 50%;
  left: -3%;
}
.box {
  display: flex;
  width: 600px;
  height: 500px;

  .animationIn {
    animation: myAnimationIn 1s;
  }

  .animationOut {
    animation: myAnimationOut 1s forwards;
  }

  .left {
    width: 100px;
    background: rgba(255, 123, 0, 0.358);
    overflow: hidden;
    margin-right: 10px;

  }

  .right {
    flex: 1;
    background: rgba(255, 255, 0, 0.646);

    /* 父元素设置相对定位 */
    position: relative;

  }

  @keyframes myAnimationIn {
    0% {
      width: 0;
    }

    100% {
      width: 100px;
    }
  }

  @keyframes myAnimationOut {
    0% {
      width: 100px;
    }

    100% {
      width: 0;
    }
  }
}
</style>

效果:

收缩与展开

如果您感觉有用,请 点赞 + 收藏 + 评论 就是对我最大的支持哟 !!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值