🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
~~~ <!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"> <!-- <transition>--> <!-- <div v-show="show">Hello World</div>--> <!-- </transition>--> <fade :show="show"> <div>Hello World</div> </fade> <fade :show="show"> <div>Bye World</div> </fade> <button @click="handleBtnClick">toggle</button> </div> <script> Vue.component('fade', { props: ['show'], // template: ` // <transition> // <slot v-if="show"></slot> // </transition> template: ` <transition @before-enter="handleBeforEnter" @enter="handleEnter"> <slot v-if="show"></slot> </transition> `, methods: { handleBeforEnter(el) { el.style.color = 'red' }, handleEnter(el, done) { setTimeout(() => { el.style.color = 'green' done() }, 2000) } } }) var vm = new Vue({ el: '#root', data: { show: true }, methods: { handleBtnClick() { this.show = !this.show } } }) </script> </body> </html> ~~~