Vue中的动画封装

JavaScript 2019-03-21 1907 0
08:13:17

Vue中的动画封装

如果我们想多次使用某个动画效果,我们可以给它做一个封装,以达到复用的效果。

首先

我们把效果写在子组件的模板里:

  1. Vue.component('child',{
  2.         template: '<transition @enter="enter" @leave="leave"><slot></slot></transition>',
  3.         methods: {
  4.             enter: function (el,done) {
  5.                 Velocity(el, { rotateX: '100deg' }, { loop: 2 ,complete: done })
  6.             },
  7.             leave: function (el,done) {
  8.                 Velocity(el, { rotateX: '100deg' }, { loop: 2 ,complete: done })
  9.             }
  10.         }
  11.     })

 然后

将需要进行动画显示的内容写在组件标签内:

  1. <button @click="see=!see">点我</button>
  2.     <child>
  3.         <p v-if="see">我是动画!</p>
  4. </child>
注意

动画效果使用的velocity.js库,所以不要忘记了在head中引用。

 

发表评论