Vue中多个元素或组件的过渡

1、多个元素的过渡:使用key防止dom复用,使用mode指定哪个效果先展示;
2、多个组件之间的过渡:使用动态组件;

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

<head>
  <meta charset="utf-8">
  <title>Vue中多个元素的过渡</title>
  <script src="./vue.js"></script>
  <style media="screen">
    .fade-enter,.v-leave-to{
      opacity:0;
    }
    .fade-enter-active,.fade-leave-active{
      transition:opacity 1s;
    }
  </style>
</head>

<body>
  <div id="root">
    <transition name="fade" mode="out-in">
      <div v-if="show" key="hello">hello world</div>
      <div v-else key="bye">bye 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
        }
      }
    })
  </script>
</body>

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

<head>
  <meta charset="utf-8">
  <title>Vue中多个组件的过渡</title>
  <script src="./vue.js"></script>
  <style media="screen">
    .fade-enter,.v-leave-to{
      opacity:0;
    }
    .fade-enter-active,.fade-leave-active{
      transition:opacity 1s;
    }
  </style>
</head>

<body>
  <div id="root">
    <transition name="fade" mode="out-in">
      <component :is="type"></component>
    </transition>
    <button @click="handleClick">change</button>
  </div>
  <script type="text/javascript">
    Vue.component('child', {
      template: '<div>child</div>'
    })
    Vue.component('child-one', {
      template: '<div>child-one</div>'
    })
    var vm = new Vue({
      el: '#root',
      data: {
        type: 'child'
      },
      methods: {
        handleClick: function() {
          this.type = this.type === 'child' ? 'child-one' : 'child'
        }
      }
    })
  </script>
</body>

</html>

推荐阅读更多精彩内容