vue實(shí)現(xiàn)多個(gè)元素或多個(gè)組件之間動(dòng)畫效果
本文實(shí)例為大家分享了vue實(shí)現(xiàn)多個(gè)元素或多個(gè)組件之間動(dòng)畫的具體代碼,供大家參考,具體內(nèi)容如下
多個(gè)元素的過渡
<style>
.v-enter,.v-leave-to{
opacity: 0;
}
.v-enter-acitve,.v-leave-active{
opacity: opacity 1s;
}
</style>
<div id='app'>
<transition>
<div v-if='show'>hello world</div>
<div v-else>bye world</div>
</transition>
<button @click='handleClick'>切換</button>
</div>
<script>
var vm = new Vue({
el:'#app',
data:{
show:true
},
methods:{
handleClick:function(){
this.show = !this.show;
}
}
})
</script>
按照之前的寫法方式,漸隱漸出的效果并沒有出現(xiàn)該有的效果,那么為什么呢?
在if else兩個(gè)元素切換的時(shí)候,會(huì)盡量的復(fù)用dom,正是vue,dom的復(fù)用,導(dǎo)致動(dòng)畫的效果不會(huì)出現(xiàn),如果想要vue不去復(fù)用dom,之前也說過,怎么做呢,給兩個(gè)div不同的key值就行了
<div v-if='show' key='hello'>hello world</div> <div v-else key='bye'>bye world</div>
這樣就可以有個(gè)明顯的動(dòng)畫效果,多個(gè)元素過渡動(dòng)畫的效果。
transition還提供了一個(gè)mode屬性,in-out是先顯示再隱藏,out-in是先隱藏再顯示
多個(gè)組件的過渡
<style>
.v-enter, .v-leave-to {
opacity: 0;
}
.v-enter-acitve, .v-leave-active {
transition: opacity 1s;
}
</style>
<div id='app'>
<transition mode='out-in'>
<child v-if='show'></child>
<child-one v-else></child-one>
</transition>
<button @click='handleClick'>切換</button>
</div>
<script>
Vue.component('child',{
template:'<div>child</div>'
})
Vue.component('child-one',{
template:'<div>child-one</div>'
})
var vm = new Vue({
el:'#app',
data:{
show:true
},
methods:{
handleClick:function(){
this.show = !this.show;
}
}
})
</script>
這個(gè)就是多個(gè)組件的過渡,采用的是上面的方式,替換子組件,那么我們換一種方式,用動(dòng)態(tài)組件
<style>
.v-enter, .v-leave-to {
opacity: 0;
}
.v-enter-acitve, .v-leave-active {
transition: opacity 1s;
}
</style>
<div id='app'>
<transition mode='out-in'>
<component :is='type'></component>
</transition>
<button @click='handleClick'>切換</button>
</div>
<script>
Vue.component('child',{
template:'<div>child</div>'
})
Vue.component('child-one',{
template:'<div>child-one</div>'
})
var vm = new Vue({
el:'#app',
data:{
type:'child'
},
methods:{
handleClick:function(){
this.type = (this.type === 'child' ? 'child-one' : 'child')
}
}
})
</script>
這樣也實(shí)現(xiàn)了多個(gè)組件的過渡效果。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
uniapp+vue3路由跳轉(zhuǎn)傳參的實(shí)現(xiàn)
本文主要介紹了uniapp+vue3路由跳轉(zhuǎn)傳參的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
Vue 3 + Element Plus樹形表格全選多選及子節(jié)點(diǎn)勾選的問題解決方
在本文中,我們解決了Vue 3和Element Plus樹形表格中的全選、多選、子節(jié)點(diǎn)勾選和父節(jié)點(diǎn)勾選等常見問題,通過逐步實(shí)現(xiàn)這些功能,您可以構(gòu)建功能強(qiáng)大且用戶友好的樹形表格組件,以滿足各種數(shù)據(jù)展示需求,對Vue 3 Element Plus樹形表格相關(guān)知識(shí)感興趣的朋友一起看看吧2023-12-12
Vite項(xiàng)目搭建與環(huán)境配置的完整版教程
Vite?使用?Rollup?作為默認(rèn)的構(gòu)建工具,可以將應(yīng)用程序的源代碼打包成一個(gè)或多個(gè)優(yōu)化的靜態(tài)文件,本文就來為大家介紹一下Vite如何進(jìn)行項(xiàng)目搭建與環(huán)境配置吧2023-09-09
vue實(shí)現(xiàn)自動(dòng)滑動(dòng)輪播圖片
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)自動(dòng)滑動(dòng)輪播圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03

