温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

动画——Vue中的动画封装

发布时间:2020-06-20 15:58:24 来源:网络 阅读:532 作者:梁十八 栏目:web开发

封装动画让代码可复用

如下是一个简单的点击渐变、渐隐:

<!DOCTYPE html> <html> <head>     <title></title>     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />     <link rel="stylesheet" type="text/css" href="./animate.css">     <script src="./vue.js"></script>     <!-- <script src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script> -->     <style type="text/css">         /*因为没有给它命名,所以用默认v开头的class名*/         .v-enter, .v-leave-to {             opacity: 0;         }         .v-enter-active, .v-leave-active {             transition: opacity 1s;         }     </style> </head> <body>     <div id="root">         //性能考虑,尽量不用index作为key值 <br>         <transition>             <div v-show="show">hello</div>         </transition>         <button @click="handBtnClick">toggle</button>     </div>     <script type="text/javascript">         var vm = new Vue({             el: "#root",             data: {                 show: true             },             methods: {                 handBtnClick: function() {                     this.show = !this.show                 }             }         });     </script> </body> </html>

封装后:

<!DOCTYPE html> <html> <head>     <title></title>     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />     <link rel="stylesheet" type="text/css" href="./animate.css">     <script src="./vue.js"></script>     <!-- <script src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script> -->     <style type="text/css">         /*因为没有给它命名,所以用默认v开头的class名*/         .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>hello world</div>         </fade>         <fade :show="show">             <h2>hello world</h2>         </fade>         <button @click="handBtnClick">toggle</button>     </div>     <script type="text/javascript">         Vue.component("fade", {             props: ["show"],             template: `                 <transition>                     <slot v-if="show"></slot>                 </transition>             `         });         var vm = new Vue({             el: "#root",             data: {                 show: true             },             methods: {                 handBtnClick: function() {                     this.show = !this.show                 }             }         });     </script> </body> </html>

其实css也可以封装:可以不用css动画,而用js动画:

<!DOCTYPE html> <html> <head>     <title></title>     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />     <link rel="stylesheet" type="text/css" href="./animate.css">     <script src="./vue.js"></script>     <!-- <script src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script> -->     <style type="text/css">         /*因为没有给它命名,所以用默认v开头的class名*/         .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>hello world</div>         </fade>         <fade :show="show">             <h2>hello world</h2>         </fade>         <button @click="handBtnClick">toggle</button>     </div>     <script type="text/javascript">         Vue.component("fade", {             props: ["show"],             template: `                 <transition @before-enter="handleBeforeEnter" @enter="handleEnter">                     <slot v-if="show"></slot>                 </transition>             `,             methods: {                 handleBeforeEnter: function(el) {                     el.style.color = "red"                 },                 handleEnter: function(el, done) {                     setTimeout(()=>{                         el.style.color = "green"                         done() //再次注意:要记得done()                     }, 1000)                 }             }         });         var vm = new Vue({             el: "#root",             data: {                 show: true             },             methods: {                 handBtnClick: function() {                     this.show = !this.show                 }             }         });     </script> </body> </html>

(这种封装可以完整的将css、js、html都封装在一个组件中。推荐

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI