Vue中的动画封装

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

<head>
  <meta charset="utf-8">
  <title>Vue中的动画封装</title>
  <script src="./vue.js"></script>
</head>

<body>
  <div id="root">
    <fade :show="show">
      <div v-show="show">hello world</div>
    </fade>
    <fade :show="show">
      <h1 v-show="show">hello world</h1>
    </fade>
    <button @click="handleBtnClick">change</button>
  </div>
  <script type="text/javascript">
    Vue.component('fade', {
      props: ['show'],
      template: `
        <transition @before-enter="handleBeforeEnter" @enter="handleEnter">
          <slot v-if="show"></slot>
        </transition>
      `,
      methods: {
        handleBeforeEnter: function(el) {
          el.style.color = 'red'
        },
        handleEnter: function(el, done) {
          setTimeout(() => {
            el.style.color = 'green'
            done()
          }, 2000)
        }
      }
    })
    var count = 0
    var vm = new Vue({
      el: '#root',
      data: {
        show: false
      },
      methods: {
        handleBtnClick: function() {
          this.show = !this.show
        }
      }
    })
  </script>
</body>

</html>

推荐阅读更多精彩内容