相對(duì)于vue1.0來(lái)說(shuō)函匕,vue2.0的動(dòng)畫(huà)變化還是挺大的,在1.0中蚪黑,直接在元素中加 transition 盅惜,后面跟上名字。而在vue2.0中忌穿,需要把設(shè)置動(dòng)畫(huà)的元素抒寂、路由放在<transition name="fade"></transition>
標(biāo)簽中,name就是動(dòng)畫(huà)名稱(chēng)掠剑。
在1.0時(shí)屈芜,css需要設(shè)置(動(dòng)畫(huà)名稱(chēng)以fade為例).fade-transition .fade-enter .fade-leave
vue1.0動(dòng)畫(huà)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>vue1.0動(dòng)畫(huà)</title>
<script type="text/javascript" src="js/vue.js"></script>
<style>
.oDiv {
width: 200px;
height: 200px;
border: 3px dashed red;
background: coral;
}
.fade-transition {
transition: 2s all ease;
}
.fade-enter {
opacity: 0;
}
.fade-leave {
opacity: 0;
transform: translate(200px);
}
</style>
</head>
<body>
<div id="box">
<input type="button" value="button" @click="toggle()"/>
<div class="oDiv" v-show="Dist" transition="fade">{{Dist}}</div>
</div>
</body>
<script type="text/javascript">
new Vue({
el: '#box',
data: {
Dist: false
},
methods: {
toggle: function () {
this.Dist = !this.Dist;
}
}
})
</script>
</html>
在2.0時(shí),css設(shè)置大改朴译,.fade-enter{}
元素初始狀態(tài) .fade-enter-active{}
元素最終狀態(tài) .fade-leave-active{}
元素離開(kāi)的最終狀態(tài)
vue2.0動(dòng)畫(huà)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>vue2.0動(dòng)畫(huà)</title>
<script type="text/javascript" src="js/vue.js"></script>
<style>
p{
width: 300px;
height: 300px;
background-color: yellow;
}
.donghua-enter-active,
.donghua-leave-active{
transition: 5s all ease;
}
.donghua-enter-active{
opacity: 1;
width: 300px;
height: 300px;
}
.donghua-leave-active{
opacity: 0;
width: 100px;
height: 100px;
}
.donghua-enter,.donghua-leave{
opacity: 0;
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div id="box">
<input type="button" value="點(diǎn)擊顯示隱藏" @click="show=!show">
<transition name="donghua">
<p v-show="show"></p>
</transition>
</div>
</body>
<script type="text/javascript">
new Vue({
el: '#box',
data: {
show: false
}
})
</script>
</html>
在2.0中井佑,依然可以與animate.css結(jié)合起來(lái)一起寫(xiě)動(dòng)畫(huà)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" type="text/css" href="css/animate.min.css"/>
<title>配合animate.css做動(dòng)畫(huà)</title>
<script type="text/javascript" src="js/vue.js"></script>
<style>
p{
width: 300px;
height: 300px;
background-color: yellow;
margin: 0 auto;
}
</style>
</head>
<body>
<div id="box">
<input type="button" value="點(diǎn)擊顯示隱藏" @click="show=!show">
<transition enter-active-class="zoomOutDown" leave-active-class="zoomOutUp">
<p v-show="show" class="animated"></p>
</transition>
</div>
</body>
<script type="text/javascript">
new Vue({
el: '#box',
data: {
show: false
}
})
</script>
</html>
在2.0中,關(guān)于動(dòng)畫(huà)還為我們提供了一些事件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>動(dòng)畫(huà)事件</title>
<script type="text/javascript" src="js/vue.js"></script>
<style>
p{
width: 300px;
height: 300px;
background-color: yellow;
}
.donghua-enter-active,
.donghua-leave-active{
transition: 5s all ease;
}
.donghua-enter-active{
opacity: 1;
width: 300px;
height: 300px;
}
.donghua-leave-active{
opacity: 0;
width: 100px;
height: 100px;
}
.donghua-enter,.donghua-leave{
opacity: 0;
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div id="box">
<input type="button" value="點(diǎn)擊顯示隱藏" @click="show=!show">
<transition name="donghua"
@before-enter="beforeEnter"
@enter="Enter"
@after-enter="afterEnter"
@before-leave="beforeLeave"
@leave="Leave"
@after-leave="afterLeave"
>
<p v-show="show"></p>
</transition>
</div>
</body>
<script type="text/javascript">
new Vue({
el: '#box',
data: {
show: false
},
methods: {
beforeEnter() {
console.log("<!-- 進(jìn)入動(dòng)畫(huà)開(kāi)始之前 -->");
},
Enter() {
console.log("<!-- 正式進(jìn)入動(dòng)畫(huà) -->");
},
afterEnter(el) {
console.log("<!-- 動(dòng)畫(huà)結(jié)束 -->");
},
beforeLeave(el) {
console.log("<!-- 離開(kāi)動(dòng)畫(huà)開(kāi)始之前 -->");
el.style.background='blue'; //改變顏色
el.innerHTML="123";
},
Leave(){
console.log("<!-- 正在離開(kāi)動(dòng)畫(huà)-->");
},
afterLeave(){
console.log("<!-- 離開(kāi)動(dòng)畫(huà)結(jié)束 -->");
},
}
})
</script>
</html>