初识 Vue(25)---( Vue 中的 JS 动画与 Velocity.js 的结合)

Vue 中的 JS 动画与 Velocity.js 的结合

通过 JS 钩子添加动画(入场)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title> Vue 中的 JS 动画与 Velocity.js 的结合</title>
    <script src = './vue.js'></script>
</head>
<body>
    <div id ='root'>
      <transition name="fade" @before-enter="handleBeforeEnter">
        <div v-show ="show">Hello World</div>
      </transition>
      <button @click="handleClick">toggle</button>
    </div>

    <script>

       var vm = new Vue({
        el:'#root',
        data:{
            show:true
        },
        methods:{
            handleClick:function(){
                this.show = !this.show

            },
            handleBeforeEnter:function(){
                console.log('beforeEnter')
            }
        }
       })
    </script>   
</body>
</html>

输出:点击---隐藏---点击---出现

          

绑定事件

 <transition name="fade" @before-enter="handleBeforeEnter">

定义事件

methods:{
            handleClick:function(){
                this.show = !this.show

            },
            handleBeforeEnter:function(){
                console.log('beforeEnter')
            }
        }

添加动画钩子(@before-enter="handleBeforeEnter"

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title> Vue 中的 JS 动画与 Velocity.js 的结合</title>
    <script src = './vue.js'></script>
</head>
<body>
    <div id ='root'>
      <transition name="fade" @before-enter="handleBeforeEnter">
        <div v-show ="show">Hello World</div>
      </transition>
      <button @click="handleClick">toggle</button>
    </div>

    <script>

       var vm = new Vue({
        el:'#root',
        data:{
            show:true
        },
        methods:{
            handleClick:function(){
                this.show = !this.show

            },
            handleBeforeEnter:function(el){
                el.style.color = 'red'
            }
        }
       })
    </script>   
</body>
</html>

输出:点击---隐藏---点击---出现(红色)

          

重新定义事件

  methods:{
            handleClick:function(){
                this.show = !this.show

            },
            handleBeforeEnter:function(el){
                el.style.color = 'red'
            }
        }

添加动画钩子(@enter="handleEnter"

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title> Vue 中的 JS 动画与 Velocity.js 的结合</title>
    <script src = './vue.js'></script>
</head>
<body>
    <div id ='root'>
      <transition name="fade" 
      @before-enter="handleBeforeEnter"
      @enter="handleEnter"
      >
        <div v-show ="show">Hello World</div>
      </transition>
      <button @click="handleClick">toggle</button>
    </div>

    <script>

       var vm = new Vue({
        el:'#root',
        data:{
            show:true
        },
        methods:{
            handleClick:function(){
                this.show = !this.show

            },
            handleBeforeEnter:function(el){
                el.style.color = 'red'
            },
            handleEnter:function(el,done){
                setTimeout(()=>{
                   el.style.color = 'green'
                    done()
                },2000)
            }
        }
       })
    </script>   
</body>
</html>

输出:点击---隐藏---点击---出现(红色)--2秒后绿色(通过JS实现动画效果)

            

重新定义事件(done():手动告诉 vue ,回调函数已经执行完毕)

methods:{
            handleClick:function(){
                this.show = !this.show

            },
            handleBeforeEnter:function(el){
                el.style.color = 'red'
            },
            handleEnter:function(el,done){
                setTimeout(()=>{
                   el.style.color = 'green'
                   done()
                },2000)
            }
        }

添加动画钩子(@enter="handleEnter"

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title> Vue 中的 JS 动画与 Velocity.js 的结合</title>
    <script src = './vue.js'></script>
</head>
<body>
    <div id ='root'>
      <transition name="fade" 
      @before-enter="handleBeforeEnter"
      @enter="handleEnter"
      @after-enter="handleAfterEnter"
      >
        <div v-show ="show">Hello World</div>
      </transition>
      <button @click="handleClick">toggle</button>
    </div>

    <script>

       var vm = new Vue({
        el:'#root',
        data:{
            show:true
        },
        methods:{
            handleClick:function(){
                this.show = !this.show

            },
            handleBeforeEnter:function(el){
                el.style.color = 'red'
            },
            handleEnter:function(el,done){
                setTimeout(()=>{
                   el.style.color = 'green'
                   
                },2000)
                    setTimeout(()=> {
                        done()
                    },4000)
            },
            handleAfterEnter:function(el){
                el.style.color = 'yellow'
            }
        }
       })
    </script>   
</body>
</html>

输出:点击---隐藏---点击---出现(红色)--2秒后绿色--再2秒黄色(通过JS实现动画效果)

              

重新定义事件(给done()添加定时器)

methods:{
            handleClick:function(){
                this.show = !this.show

            },
            handleBeforeEnter:function(el){
                el.style.color = 'red'
            },
            handleEnter:function(el,done){
                setTimeout(()=>{
                   el.style.color = 'green'
                   
                },2000)
                    setTimeout(()=> {
                        done()
                    },4000)
            },
            handleAfterEnter:function(el){
                el.style.color = 'yellow'
            }
        }

总结:入场动画钩子:@before-enter="handleBeforeEnter" /  @enter="handleEnter" /  @after-enter="handleAfterEnter"

出场动画钩子:@before-leave="handleBeforeLeave" /  @enter="handleLeave" /  @after-leave="handleAfterLeave"

与 Velocity.js 结合

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title> Vue 中的 JS 动画与 Velocity.js 的结合</title>
    <script src = './vue.js'></script>
    <script src = './velocity.js'></script>
</head>
<body>
    <div id ='root'>
      <transition name="fade" 
      @before-enter="handleBeforeEnter"
      @enter="handleEnter"
      @after-enter="handleAfterEnter"
      >
        <div v-show ="show">Hello World</div>
      </transition>
      <button @click="handleClick">toggle</button>
    </div>

    <script>

       var vm = new Vue({
        el:'#root',
        data:{
            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
                    })
            },
            handleAfterEnter:function(el){
               console.log("动画结束") 
            }
        }
       })
    </script>   
</body>
</html>

输出:点击---隐藏---逐渐出现(1秒)--执行“动画结束”

          

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值