Vue中的JS动画与Velocity.js的结合

通过Velocity.js使用一些简单的语法实现动态效果

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

<head>
  <meta charset="utf-8">
  <title>Vue中的JS动画</title>
  <script src="./vue.js"></script>
  <link rel="stylesheet" href="./animate.css">
  <style media="screen">
  </style>
</head>

<body>
  <div id="root">
    <transition name="fade" @before-enter="handleBeforeEnter" @enter="handleEnter" @after-enter="handleAfterEnter">
      <div v-show="show">hello world</div>
    </transition>
    <button @click="handleClick">change</button>
  </div>
  <script type="text/javascript">
    var vm = new Vue({
      el: '#root',
      data: {
        show: true
      },
      methods: {
        handleClick: function() {
          this.show = !this.show
        },
        handleBeforeEnter: function(el) {
          el.style.color = 'red'
        },
        handleEnter: function(el, done) {
          setTimeout(() => {
            el.style.color = 'green'
          }, 2000)
          setTimeout(()=>{
            done()
          },4000)
        },
        handleAfterEnter:function(el){
          el.style.color="#000"
        }
      }
    })
  </script>
</body>

</html>
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>Vue中的JS动画与Velocity.js的结合</title>
  <script src="./vue.js"></script>
  <script type="text/javascript" src="./velocity.js">

  </script>
</head>

<body>
  <div id="root">
    <transition name="fade" @before-enter="handleBeforeEnter" @enter="handleEnter" @after-enter="handleAfterEnter">
      <div v-show="show">hello world</div>
    </transition>
    <button @click="handleClick">change</button>
  </div>
  <script type="text/javascript">
    var vm = new Vue({
      el: '#root',
      data: {
        show: true
      },
      methods: {
        handleClick: function() {
          this.show = !this.show
        },
        handleBeforeEnter: function(el) {
          el.style.opacity = 0;
        },
        handleEnter: function(el, done) {
          Velocity(el, {
            opacity: 1
          }, {
            duration: 1000,
            complete: done
          })
        },
        handleAfterEnter: function(el) {
          console.log("动画结束")
        }
      }
    })
  </script>
</body>

</html>

推荐阅读更多精彩内容