聊聊关于Vue的动画效果

272 阅读1分钟

1.1 动画属性实现

通过自己动手写原生animate,借助Vue的transition标签来实现效果

xxx.vue

<button @click="isShow=!isShow">演示动画</button>
// appear 第一次渲染就有效果 name是标识当前元素的  不写默认找 v-enter-active
<transition name="hello" appear>    
    <h1 v-show="isShow">hello</h1>
</transition>
--------------------------------
data () {
    return {
        isShow:true
    }
}
---------------------------------
<style scoped>    
    .hello-enter-active{
        animation: hhh 0.5s linear;
    }
​
    .hello-leave-active{
        animation: hhh 0.5s linear reverse;
    }
​
    @keyframes hhh {
        from{
            transform: translateX(-100%);
                                  }
        to{
            transform: translateX(0px);
        }
    }
</style>

2.1 过渡属性实现

通过写Vue定义好的样式(v-enter,v-leave等),借助transition标签来实现效果

xxx.vue

<button @click="isShow=!isShow">演示动画</button>
<transition name="hello" appear>    
    <h1 v-show="isShow">hello</h1>
</transition>
--------------------------------
data () {
    return {
        isShow:true
    }
}
---------------------------------
<style scoped>    
   /* 进入的起点、离开的终点 */
  .hello-enter,.hello-leave-to{
    transform: translateX(-100%);
  }
  /* 进入、离开的时间 速度等 */
  .hello-enter-active,.hello-leave-active{
    transition: 0.5s linear;
  }
  /* 进入的终点、离开的起点 */
  .hello-enter-to,.hello-leave{
    transform: translateX(0);
  }
</style>

3.1 第三方动画库 Animate.css

通过使用Animate.css,借助transition标签来实现效果 (不需要你写任何样式,用人家的就好),当然还有很多第三方的动画库,需要用到自行百度(Ctrl c ,v大法)

具体使用参考:animate.style/

Step1: npm install animate.css --save   //安装
Step2   :  import 'animate.css'     //引入css
​
Step3:<h1 class="animate__animated animate__bounce"></h1> //使用类名
Step3:enter-active-class leave-active-class // 使用具体的类型 去官网查看你要的效果,复制类名即可

xxx.vue

<button @click="isShow=!isShow">演示动画</button>
<transition
    appear
    name="animate__animated animate__bounce"
    enter-active-class="animate__backOutLeft"   //进入的动画
    leave-active-class="animate__jello"         //离开的动画
>
    <h1 v-show="isShow">hello</h1>
</transition>
---------------------------------
data () {
    return {
        isShow:true
    }
}

4.1 多个标签实现类似动画

需求:多个标签实现相同动画,但是效果互斥,这时候就需要使用 transition-group 标签

xxx.vue

//用Animate.css来举例,上面的三种方案都是一样的  使用transition-group包裹,但是记得每项加上key
 <transition-group
    appear
    name="animate__animated animate__bounce"
    enter-active-class="animate__backOutLeft"
    leave-active-class="animate__jello"
>
   <h1 v-show="!isShow" key="1">hello</h1>  //key是每个节点的标识,就是diff找的key
   <h1 v-show="isShow" key="2">hello</h1>
</transition-group>
---------------------------------
data () {
    return {
        isShow:true
    }
}

如果觉得有帮助欢迎点赞、评论。 上述内容仅仅代表个人观点,如有错误,请指正。如果你也对前端感兴趣,欢迎访问我的个人博客sundestiny.github.io/myblog/