在vue中使用velocity动画库实现列表交错过渡

这个消息通知组件,我点击清空通知后,想要清除里面的信息,如果直接清空是下面这样的:

未加动画的效果

可以看出这样会显得非常生硬,再来看加上过渡之后的:

加上动画之后的效果

是不是感觉不一样了,这实际上是用velocity动画库实现的:
  1. 首先npm安装Velocity
npm i velocity-animate
  1. 在main中引入velocity-animate就可以了
import Velocity from 'velocity-animate'
Vue.prototype.Velocity = Velocity
  1. 这是我根据vue官网中的例子修改的
<template>
  <div id="app">
    <button @click="show = !show">{{ btnText }}</button>
    <transition-group name="staggered-fade" tag="ul" v-bind:css="false" v-on:before-enter="beforeEnter"
      v-on:enter="enter" v-on:leave="leave">
      <li class="app-li" v-show="show" v-for="(item, index) in list" :key="item.id" :data-index="index">{{ item.msg }}
      </li>
    </transition-group>
  </div>
</template>
<script>
export default {
  data() {
    return {
      show: true,
      btnText: "退出过渡",
      list: [
        { id: '01', msg: 'Bruce Lee' },
        { id: '02', msg: 'Jackie Chan' },
        { id: '03', msg: 'Chuck Norris' },
        { id: '04', msg: 'Jet Li' },
        { id: '05', msg: 'Kung Fury' }
      ]
    }
  },
  methods: {
    //过渡初始参数
    beforeEnter: function (el) {
      el.style.opacity = 0
      el.style.height = 0
    },
    //进入过渡动画
    enter: function (el, done) {
      var delay = el.dataset.index * 150
      this.btnText = "退出过渡"
      setTimeout(function () {
        Velocity(
          el,
          { opacity: 1, height: '30px' },//注意这里高度要跟你li高度一样
          { complete: done }
        )
      }, delay)
    },
    //离开过渡动画
    leave: function (el, done) {
      var delay = el.dataset.index * 150
      this.btnText = "进入过渡"
      setTimeout(function () {
        Velocity(
          el,
          { opacity: 0, height: 0 },
          { complete: done }
        )
      }, delay)
    }
  }
}
</script>
<style>
.app-li {
  list-style: none;
  width: 200px;
  height: 30px;
  border: 1px solid #333;
}
</style>
``
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值