Vue中多個(gè)元素、組件的過渡及列表過渡的方法示例
多個(gè)元素之間過渡動(dòng)畫效果
多元素之間如何實(shí)現(xiàn)過渡動(dòng)畫效果呢?看下面代碼
.fade-enter,
.fade-leave-to{
opacity: 0;
}
.fade-enter-active,
.fade-leave-active{
transition: opacity 3s;
}
<div id="root">
<transition name="fade">
<div v-if="show">hello world</div>
<div v-else>bye world</div>
</transition>
<button @click="handleClick">切換</button>
</div>
let vm = new Vue({
el: '#root',
data: {
show: true
},
methods: {
handleClick() {
this.show = !this.show
}
}
})
這么寫行不行呢?肯定是不行的,因?yàn)?Vue 在兩個(gè)元素進(jìn)行切換的時(shí)候,會(huì)盡量復(fù)用dom,就是因?yàn)檫@個(gè)原因,導(dǎo)致現(xiàn)在動(dòng)畫效果不會(huì)出現(xiàn)。
如果不讓 Vue 復(fù)用dom的話,應(yīng)該怎么做呢?只需要給這兩個(gè)div不同的key值就行了
<div v-if="show" key="hello">hello world</div> <div v-else key="bye">bye world</div>
這個(gè)時(shí)候當(dāng)div元素進(jìn)行切換的時(shí)候,就不會(huì)復(fù)用了。
mode
Vue 提供了一mode屬性,來實(shí)現(xiàn)多個(gè)元素切換時(shí)的效果
mode取值in-out,動(dòng)畫效果是先出現(xiàn)在隱藏
<div id="root">
<transition name="fade" mode="in-out"> //第一次點(diǎn)擊時(shí),執(zhí)行順序?yàn)椋孩佗?
<div v-if="show">hello world</div> //再消失 ②
<div v-else>bye world</div> //先顯示 ①
</transition>
<button @click="handleClick">切換</button>
</div>
mode取值為out-in,動(dòng)畫效果為先隱藏在出現(xiàn)
<div id="root">
<transition name="fade" mode="out-in"> //第一次點(diǎn)擊時(shí),執(zhí)行順序?yàn)椋孩佗?
<div v-if="show">hello world</div> //先消失 ①
<div v-else>bye world</div> //再顯示 ②
</transition>
<button @click="handleClick">切換</button>
</div>
多個(gè)組件之間過渡動(dòng)畫效果
這里需要借助動(dòng)態(tài)組件來實(shí)現(xiàn)多組件之間過渡動(dòng)畫效果
先用普通的方式來實(shí)現(xiàn)切換:
.fade-enter,
.fade-leave-to{
opacity: 0;
}
.fade-enter-active,
.fade-leave-active{
transition: opacity 1s;
}
<div id="root">
<transition name="fade" mode="in-out">
<child-one v-if="show"></child-one>
<child-two v-else></child-two>
</transition>
<button @click="handleClick">切換</button>
</div>
Vue.component('child-one',{
template:'<div>child-one</div>'
})
Vue.component('child-two',{
template:'<div>child-two</div>'
})
let vm = new Vue({
el: '#root',
data: {
show: true
},
methods: {
handleClick() {
this.show = !this.show
}
}
})
你會(huì)發(fā)現(xiàn),這樣子實(shí)現(xiàn)組件切換,transition動(dòng)畫效果是存在的,但是我們想要用動(dòng)態(tài)組件來實(shí)現(xiàn),該怎么弄呢?
可查看之前的文章:Vue 動(dòng)態(tài)組件與 v-once 指令,這篇文章中詳細(xì)的介紹了 Vue 的動(dòng)態(tài)組件
列表過渡
這里需要使用一個(gè)新標(biāo)簽transition-group來是實(shí)現(xiàn)
.fade-enter,
.fade-leave-to{
opacity: 0;
}
.fade-enter-active,
.fade-leave-active{
transition: opacity 1s;
}
<div id="root">
<transition-group name="fade">
<div v-for="item of list" :key="item.id">{{item.title}}-----{{item.id}}</div>
</transition-group>
<button @click="handleClick">添加</button>
</div>
let vm = new Vue({
el: '#root',
data: {
count:0,
list:[]
},
methods: {
handleClick() {
this.list.push({
id:this.count ++,
title:'hello world'
})
}
}
})
為什么使用了transition-group標(biāo)簽后,就可以出現(xiàn)過渡動(dòng)畫效果了呢?看下面代碼:
<transition-group name="fade"> <div>hello world</div> <div>hello world</div> <div>hello world</div> </transition-group>
在循環(huán)過后,頁面中會(huì)出現(xiàn)一個(gè)個(gè)div元素,如果你在外面添加一個(gè)transition-group的標(biāo)簽,相當(dāng)于你在每一個(gè)div外層都加了一個(gè)transition標(biāo)簽,看下面代碼
<transition> <div>hello world</div> </transition> <transition> <div>hello world</div> </transition> <transition> <div>hello world</div> </transition>
這時(shí)候,Vue 把列表的過渡轉(zhuǎn)化為單個(gè)的div元素的過渡了,Vue 會(huì)在這個(gè)元素隱藏或者顯示的時(shí)候動(dòng)態(tài)的找到時(shí)間點(diǎn),增加對(duì)應(yīng)的class。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue之“} expected“和“at-rule or selector ex
這篇文章主要介紹了vue之“} expected“和“at-rule or selector expected“報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Vue?Vuex搭建vuex環(huán)境及vuex求和案例分享
這篇文章主要介紹了Vue?Vuex搭建vuex環(huán)境及vuex求和案例分享,?Vue?中實(shí)現(xiàn)集中式狀態(tài)管理的一個(gè)?Vue?插件,對(duì)?vue?應(yīng)用中多個(gè)組件的共享狀態(tài)進(jìn)行集中式的管理,下文相關(guān)介紹,需要的朋友可以參考一下2022-04-04
Vue.js的動(dòng)態(tài)組件模板的實(shí)現(xiàn)
這篇文章主要介紹了Vue.js的動(dòng)態(tài)組件模板的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11
關(guān)于vue中@click.native.prevent的說明
這篇文章主要介紹了關(guān)于vue中@click.native.prevent的說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

