封裝動畫讓代碼可複用javascript
以下是一個簡單的點擊漸變、漸隱:css
<!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>
封裝後: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"> <h1>hello world</h1> </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動畫:vue
<!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"> <h1>hello world</h1> </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都封裝在一個組件中。推薦)
java