加一個功能,當刷新頁面的時候也有動畫效果(加上appear和appear-active-class="animated rubberBand":javascript
<!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> --> </head> <body> <div id="root"> //name隨便取名: <!-- <transition name="fade"> <div v-show="show">hello</div> </transition> --> //自定義class名字: <transition name="fade" appear enter-active-class="animated rubberBand" leave-active-class="animated hinge" appear-active-class="animated rubberBand" > <div v-show="show">hello</div> </transition> <button @click="handleClick">切換</button> </div> <script type="text/javascript"> var vm = new Vue({ el: "#root", data: { show: true }, methods: { handleClick: function() { this.show = !this.show } } }); </script> </body> </html>
若是過渡和庫的動畫都有: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"> .fade-enter, .fade-leave-to { opacity: 0 } .fade-enter-active, .fade-leave-active { transition: opacity 3s } </style> </head> <body> <div id="root"> //name隨便取名: <!-- <transition name="fade"> <div v-show="show">hello</div> </transition> --> //自定義class名字: <transition name="fade" appear enter-active-class="animated rubberBand fade-enter-active" leave-active-class="animated hinge fade-leave-active" appear-active-class="animated rubberBand" > <div v-show="show">hello</div> </transition> <button @click="handleClick">切換</button> </div> <script type="text/javascript"> var vm = new Vue({ el: "#root", data: { show: true }, methods: { handleClick: function() { this.show = !this.show } } }); </script> </body> </html>
可是,問題是,animate庫的動畫時長是1s,而我本身設置的是3s,因此要統一下時間。用type="transition",意思是以transition時間爲準:html
(可是通過測試,發現有問題)vue
還能夠設置時間duration="10000",單位毫秒。還能夠設置更細:duration="{enter: 5000, leave: 1000}":java
<!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"> .fade-enter, .fade-leave-to { opacity: 0; } .fade-enter-active, .fade-leave-active { transition: opacity 3s; } </style> </head> <body> <div id="root"> //自定義class名字: <transition :duration="{enter: 5000, leave: 1000}" name="fade" appear enter-active-class="animated rubberBand fade-enter-active" leave-active-class="animated hinge fade-leave-active" appear-active-class="animated rubberBand" > <div v-show="show">hello</div> </transition> <button @click="handleClick">切換</button> </div> <script type="text/javascript"> var vm = new Vue({ el: "#root", data: { show: true }, methods: { handleClick: function() { this.show = !this.show } } }); </script> </body> </html>