@before-enter,在隐藏后,点击让它显示的时候,显示之前会触发。@enter不同,是在动画执行的过程中触发。done触发完成之后就是@after-enter。这些是通过js钩子来实现,也就是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> -->
</head>
<body>
<div id="root">
//自定义class名字:
<transition
name="fade"
@before-enter="handleBeforeEnter"
@enter="handleEnter"
@after-enter="handleAfterEnter"
>
<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
},
//el指的是动画包裹的标签
handleBeforeEnter: function(el) {
//console.log("handleBeforeEnter")
//在标签从隐藏点击显示的时候,变成了红色
el.style.color = "red"
},
//接收两个回调函数,el同上,done
handleEnter: function(el, done) {
//之前显示是红色,两秒后变成绿色
setTimeout(()=>{
el.style.color = "green"
}, 2000)
setTimeout(()=>{
//但4秒才告诉vue结束(也就是2秒后变绿,又4-2秒后变黑)
done() //这个done很重要,执行完之后要告诉vue,已经执行完
}, 4000)
},
handleAfterEnter: function(el) {
el.style.color = "black"
}
}
});
</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> -->
</head>
<body>
<div id="root">
//自定义class名字:
<transition
name="fade"
@before-leave="handleBeforeLeave"
@leave="handleLeave"
@after-leave="handleAfterLeave"
>
<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
},
//el指的是动画包裹的标签
handleBeforeLeave: function(el) {
//console.log("handleBeforeLeave")
//在标签从隐藏点击显示的时候,变成了红色
el.style.color = "red"
},
//接收两个回调函数,el同上,done
handleLeave: function(el, done) {
//之前显示是红色,两秒后变成绿色
setTimeout(()=>{
el.style.color = "green"
}, 2000)
setTimeout(()=>{
//但4秒才告诉vue结束(也就是2秒后变绿,又4-2秒后变黑)
done() //这个done很重要,执行完之后要告诉vue,已经执行完
}, 4000)
},
handleAfterLeave: function(el) {
setTimeout(()=>{
el.style.color = "black"
}, 2000)
}
}
});
</script>
</body>
</html>
使用velocity。下载地址:http://velocityjs.org/
<!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="./velocity.js"></script>
<!-- <script src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script> -->
</head>
<body>
<div id="root">
//自定义class名字:
<transition
name="fade"
@before-enter="handleBeforeEnter"
@enter="handleEnter"
@after-enter="handleAfterEnter"
>
<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
},
//el指的是动画包裹的标签
handleBeforeEnter: function(el) {
el.style.opacity = 0;
},
//接收两个回调函数,el同上,done
handleEnter: function(el, done) {
//注意,要加上complete: done告诉vue结束
Velocity(el, {opacity:1}, {duration: 1000, complete: done})
},
handleAfterEnter: function(el) {
alert("动画结束")
}
}
});
</script>
</body>
</html>