过渡

作者: 小杰的简书 | 来源:发表于2018-11-24 10:47 被阅读0次

    使用方法

    <div id="example-2">
      <button @click="show = !show">Toggle show</button>
      <transition name="bounce">
        <p v-if="show"> Pellentesque habitant morbi tristique senectus et netus.</p>
      </transition>
    </div>
    
    .bounce-enter-active {
      animation: bounce-in .5s;
    }
    .bounce-leave-active {
      animation: bounce-in .5s reverse;
    }
    @keyframes bounce-in {
      0% {
        transform: scale(0);
      }
      50% {
        transform: scale(1.5);
      }
      100% {
        transform: scale(1);
      }
    }
    

    有 6 种 class 类名会在进入/离开(enter/leave)过渡中处理

    
    ### [过渡类名(Transition Classes)](https://vue.docschina.org/v2/guide/transitions.html#过渡类名-Transition-Classes "过渡类名(Transition Classes)")
    
    1.  `v-enter`:进入式过渡(entering transition)的开始状态。在插入元素之前添加,在插入元素之后一帧移除。
    
    2.  `v-enter-active`:进入式过渡的激活状态。应用于整个进入式过渡时期。在插入元素之前添加,过渡/动画(transition/animation)完成之后移除。此 class 可用于定义进入式过渡的 duration, delay 和 easing 曲线。
    
    3.  `v-enter-to`:**仅适用于版本 2.1.8+。**进入式过渡的结束状态。在插入元素之后一帧添加(同时,移除 `v-enter`),在过渡/动画完成之后移除。
    
    4.  `v-leave`:离开式过渡(leaving transition)的开始状态。在触发离开式过渡时立即添加,在一帧之后移除。
    
    5.  `v-leave-active`:离开式过渡的激活状态。应用于整个离开式过渡时期。在触发离开式过渡时立即添加,在过渡/动画(transition/animation)完成之后移除。此 class 可用于定义离开式过渡的 duration, delay 和 easing 曲线。
    
    6.  `v-leave-to`:**仅适用于版本 2.1.8+。**离开式过渡的结束状态。在触发离开式过渡之后一帧添加(同时,移除 `v-leave`),在过渡/动画完成之后移除。
    
    

    自定义过渡class类名

    你也可以通过提供一下属性来指定自定义过渡类名
    enter-class
    enter-active-class
    enter-to-class (2.1.8+)
    leave-class
    leave-active-class
    leave-to-class (2.1.8+)
    它们将覆盖默认约定的类名,这对于将 Vue 的过渡系统和其他现有的第三方 CSS 动画库(如 [Animate.css](https://daneden.github.io/animate.css/))集成使用会非常有用。
    <link href="https://cdn.jsdelivr.net/npm/animate.css@3.5.1" rel="stylesheet" type="text/css">
    
    <div id="example-3">
      <button @click="show = !show">
        Toggle render
      </button>
      <transition
        name="custom-classes-transition"
        enter-active-class="animated tada"
        leave-active-class="animated bounceOutRight"
      >
        <p v-if="show">hello</p>
      </transition>
    </div>
    

    过度属性

    1.duration
    <transition :duration="{ enter: 500, leave: 800 }">...</transition>
    2.mode
    (1)
    in-out:新元素先过渡进入(transition in),过渡完成之后,当前元素过渡离开
    (2)
    out-in:当前元素先过渡离开(transition out),过渡完成之后,新元素过渡进入
    <transition name="fade" mode="out-in">
      <!-- ... the buttons ... -->
    </transition>
    

    钩子函数

    <script src="https://lib.baomitu.com/velocity/1.2.3/velocity.min.js"></script>
    <transition
      v-on:before-enter="beforeEnter"
      v-on:enter="enter"
      v-on:after-enter="afterEnter"
      v-on:enter-cancelled="enterCancelled"
    
      v-on:before-leave="beforeLeave"
      v-on:leave="leave"
      v-on:after-leave="afterLeave"
      v-on:leave-cancelled="leaveCancelled"
    >
    </transition>
    
     beforeEnter: function (el) {
          el.style.opacity = 0
        },
        enter: function (el, done) {
          Velocity(el, { opacity: 1, fontSize: '1.4em' }, { duration: 300 })
          Velocity(el, { fontSize: '1em' }, { complete: done })
        },
        leave: function (el, done) {
          Velocity(el, { translateX: '15px', rotateZ: '50deg' }, { duration: 600 })
          Velocity(el, { rotateZ: '100deg' }, { loop: 2 })
          Velocity(el, {
            rotateZ: '45deg',
            translateY: '30px',
            translateX: '30px',
            opacity: 0
          }, { complete: done })
        }
    

    多个组件过渡

    多个组件之间的过渡甚至更简单 - 我们不需要使用 `key` 属性。相反,我们需要使用[动态组件]
    <transition name="component-fade" mode="out-in">
      <component v-bind:is="view"></component>
    </transition>
    new Vue({
      el: '#transition-components-demo',
      data: {
        view: 'v-a'
      },
      components: {
        'v-a': {
          template: '<div>Component A</div>'
        },
        'v-b': {
          template: '<div>Component B</div>'
        }
      }
    })
    .component-fade-enter-active, .component-fade-leave-active {
      transition: opacity .3s ease;
    }
    .component-fade-enter, .component-fade-leave-to
    /* .component-fade-leave-active 在低于 2.1.8 版本中 */ {
      opacity: 0;
    }
    

    进入式/离开式列表过渡

    <div id="list-demo">
      <button v-on:click="add">Add</button>
      <button v-on:click="remove">Remove</button>
      <transition-group name="list" tag="p">
        <span v-for="item in items" v-bind:key="item" class="list-item">
          {{ item }}
        </span>
      </transition-group>
    </div>
    new Vue({
      el: '#list-demo',
      data: {
        items: [1,2,3,4,5,6,7,8,9],
        nextNum: 10
      },
      methods: {
        randomIndex: function () {
          return Math.floor(Math.random() * this.items.length)
        },
        add: function () {
          this.items.splice(this.randomIndex(), 0, this.nextNum++)
        },
        remove: function () {
          this.items.splice(this.randomIndex(), 1)
        },
      }
    })
    
    .list-item {
      display: inline-block;
      margin-right: 10px;
    }
    .list-enter-active, .list-leave-active {
      transition: all 1s;
    }
    .list-enter, .list-leave-to /* .list-leave-active 在低于 2.1.8 版本中 */ {
      opacity: 0;
      transform: translateY(30px);
    }
    

    相关文章

      网友评论

          本文标题:过渡

          本文链接:https://www.haomeiwen.com/subject/zrgjxqtx.html