【Vue3 从入门到实战 进阶式掌握完整知识体系】022-Vue中的动画:列表动画

4、列表动画

代码

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>hello vue</title>
  <!-- 引入Vue库 -->
  <script src="https://unpkg.com/vue@next"></script>
  <!-- 样式 -->
  <style>
    .v-enter-from{
      opacity: 0;
      transform: translateY(30px);
    }
    .v-enter-active{
      transition: all .5s ease-in;
    }
    .v-enter-to{
      opacity: 1;
      transform: translateY(0px);
    }
    .list-item{
      display: inline-block;
      margin-right: 10px;
    }
  </style>
</head>

<body>
  <div id="root"></div>
</body>

<script>
  const app = Vue.createApp({
    data(){return{list: [1, 2, 3, 4]}},
    methods:{
      add(){
        this.list.unshift(this.list.length + 1);
      }
    },
    template: `
        <div>
          <transition-group>
            <span class="list-item" v-for="item in list" :key="item">{{item}}</span>
          </transition-group>
          <button @click="add">添加</button>
        </div>
    `
  });

  const vm = app.mount('#root');
</script>

</html>

运行结果

image-20210614002807803.png

让其他列表项“温柔地”后移

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>hello vue</title>
  <!-- 引入Vue库 -->
  <script src="https://unpkg.com/vue@next"></script>
  <!-- 样式 -->
  <style>
    .v-enter-from{
      opacity: 0;
      transform: translateY(30px);
    }
    .v-enter-active{
      transition: all .5s ease-in;
    }
    .v-enter-to{
      opacity: 1;
      transform: translateY(0px);
    }
    .v-move{
      transition: all .5s ease-in;
    }
    .list-item{
      display: inline-block;
      margin-right: 10px;
    }
  </style>
</head>

<body>
  <div id="root"></div>
</body>

<script>
  const app = Vue.createApp({
    data(){return{list: [1, 2, 3, 4]}},
    methods:{
      add(){
        this.list.unshift(this.list.length + 1);
      }
    },
    template: `
        <div>
          <transition-group>
            <span class="list-item" v-for="item in list" :key="item">{{item}}</span>
          </transition-group>
          <button @click="add">添加</button>
        </div>
    `
  });

  const vm = app.mount('#root');
</script>

</html>

运行结果

image-20210614003105290.png

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-e2ESTE2j-1624977185497)(https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/836d42094ff34a43ab4d37d0af7441c5~tplv-k3u1fbpfcp-watermark.image)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值