美文网首页前端开发笔记
18.vue中的动画封装

18.vue中的动画封装

作者: yaoyao妖妖 | 来源:发表于2018-07-07 21:58 被阅读63次

1.动画封装之后可复用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue中的列表过渡</title>
    <script src="./vue.js"> </script>
    <style>
        .v-enter,.v-leave-to{
            opacity: 0;
        }

        .v-enter-active,v-leave-active{
            transition:opacity 1s;
        }
    </style>

</head>
<body>


   <div id="root">

         <fade :show="show">
            <div>helloworld</div> 
         </fade>

         <fade :show="show">
                <h1>he</h1> 
         </fade>

       <button @click="handleClick">add</button>   

   </div>

   <script>  

       Vue.component('fade',{
           props:['show'],
           template:'<transition><slot v-if="show">helloworld</slot> </transition>'

       })


       var count = 0;
       var app = new Vue({
           el:'#root',
           data:{
            show :false
           },
           methods:{
            handleClick:function(){
                 this.show = !this.show
            }
           }
       })

   </script>

</body>

</html>

2.样式也可以封装到动画里面(进一步封装)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue中的列表过渡</title>
    <script src="./vue.js"> </script>
    <style>
        .v-enter,.v-leave-to{
            opacity: 0;
        }

        .v-enter-active,v-leave-active{
            transition:opacity 1s;
        }
    </style>

</head>
<body>


   <div id="root">

         <fade :show="show">
            <div>helloworld</div> 
         </fade>

         <fade :show="show">
                <h1>he</h1> 
         </fade>

       <button @click="handleClick">add</button>   

   </div>

   <script>  

       Vue.component('fade',{
           props:['show'],
           template:'<transition @before-enter="handleBeforeEnter" @enter="handleEnter"><slot v-if="show">helloworld</slot> </transition>',
           methods:{
               handleBeforeEnter:function(el){
                   el.style.color = 'red';
                },

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

           }

       })


       var count = 0;
       var app = new Vue({
           el:'#root',
           data:{
            show :false
           },
           methods:{
            handleClick:function(){
                 this.show = !this.show
            }

           }

       })

   </script>

</body>

</html>

相关文章

网友评论

    本文标题:18.vue中的动画封装

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