<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Vue中动画的封装</title>
<!-- <style>
.v-enter,.v-leave-to{
opacity: 0;
}
.v-enter-active,.v-leave-active{
transition:opacity 3s;
}
</style> -->
</head>
<body>
<script src="https://vuejs.org/js/vue.js"></script>
<div id="app">
<fade :show="show">
<div>hello world</div>
</fade>
<fade :show="show">
<h1>hello world</h1>
</fade>
<button @click="chang">toggle</button>
</div>
<script>
// 创建全局组件
Vue.component('fade',{
props:['show'],
template:`<transition @before-enter="beforeEnter" @enter="handleEnter">
<slot v-if="show"></slot>
</transition>`,
methods:{
beforeEnter(el){
el.style.color="red"
},
handleEnter(el,done){
setTimeout(()=>{
el.style.color="green"
done()
},3000)
}
}
})
var vm=new Vue({
el:'#app',
data:{
show:false,
},
methods: {
chang(){
this.show=!this.show
}
}
})
</script>
</body>
</html>