在vue项目中使用js动画库velocity

在vue项目中使用js动画库velocity

在vue中通过js实现动画

在vue中通过js实现动画使用的是vue提供的动画钩子函数。

  1. before-enter:入场动画还未开始执行时
  2. enter:入场动画开始执行
  3. after-enter:入场动画执行结束
  4. before-leave:出场动画还未开始执行时
  5. enter:出场动画开始执行
  6. after-leave:出场动画执行结束
<template>
    <div>
        <transition
            name="fade"
            @before-enter="handleBeforeEnter"
            @enter="handleEnter"
            @after-enter="handleAfterEnter"
        >
            <div v-if="show">transition标签</div>
        </transition>
        <button @click="handleClick">切换</button>
    </div>
</template>
<script>
import Velocity from 'velocity-animate'
export default {
    name:"Four",
    data() {
        return {
            show:true
        }
    },
    methods: {
        handleClick:function(){
            this.show = !this.show;
        },
        handleBeforeEnter:function(el){
            //el指动画包裹的div标签
            alert("handleBeforeEnter")
            el.style.color = "red";
        },
        handleEnter:function(el,done){
            // done回掉函数,当动画执行完成之后需要手动的调用done函数,相当于告诉vue此时动画已经执行完成。
            // 当done函数调用完成之后,vue会触发另外一个函数即handleAfterEnter
            // alert("handleEnter")
            setTimeout(function(){
                el.style.color = "green";
                // done();
            },2000)
            setTimeout(function(){
                done();
            },4000)
        },
        handleAfterEnter:function(el){
            alert("handleAfterEnter");
            el.style.color = "red";
        }
}
</script>
<style scoped></style>

在vue中使用js动画库velocity

安装

cnpm install velocity-animate@beta

引用

在需要使用的组件中引用

import Velocity from 'velocity-animate'

调用

在js动画的钩子函数中使用

<template>
    <div>
        <transition
            name="fade"
            @before-enter="handleBeforeEnter"
            @enter="handleEnter"
            @after-enter="handleAfterEnter"
        >
            <div v-if="show">transition标签</div>
        </transition>
        <button @click="handleClick">切换</button>
    </div>
</template>
<script>
import Velocity from 'velocity-animate'
export default {
    name:"Four",
    data() {
        return {
            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})
            //让el元素的opacity缓慢的变成1,并耗时1s,当动画执行完毕之后complete对应的属性内容会被执行(即执行done函数)
        },
        handleAfterEnter:function(){
            alert("动画结束")
        }
    },
}
</script>
<style scoped>
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值