vue如何進(jìn)行動(dòng)畫(huà)的封裝
本文實(shí)例為大家分享了vue動(dòng)畫(huà)封裝的具體代碼,供大家參考,具體內(nèi)容如下
<style>
.v-enter,.v-leave-to{
opacity: 0;
}
.v-enter-active,.v-leave-active{
transition:opacity 1s;
}
</style>
<div id='app'>
<transition>
<div v-if='show'>hello 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>
有時(shí)候這種漸隱漸現(xiàn)的效果用的比較多,要復(fù)用,需要封裝一下,怎么封裝呢
<style>
.v-enter,.v-leave-to{
opacity: 0;
}
.v-enter-active,.v-leave-active{
transition:opacity 1s;
}
</style>
<div id='app'>
<fade :show='show'>
<div>hello world</div>
</fade>
<fade :show='show'>
<h1>hello world</h1>
</fade>
<button @click='handleClick'>切換</button>
</div>
<script>
Vue.component('fade',{
props:['show'],
template: `
<transition>
<slot v-if='show'></slot>
</transition>
`
})
var vm = new Vue({
el:'#app',
data:{
show:false
},
methods:{
handleClick:function(){
this.show = !this.show;
}
}
})
</script>
可以這樣封裝,將dom元素傳入slot,除了這樣,還可以樣式一起封裝進(jìn)去
<div id='app'>
<fade :show='show'>
<div>hello world</div>
</fade>
<fade :show='show'>
<h1>hello world</h1>
</fade>
<button @click='handleClick'>切換</button>
</div>
<script>
Vue.component('fade',{
props:['show'],
template: `
<transition @before-enter='handleBeforeEnter' @enter='handleEnter'>
<slot v-if='show'></slot>
</transition>
`,
methods:{
handleBeforeEnter:function(el){
el.style.color='red'
},
handleEnter:function(el,done){
setTimeout(()=>{
el.style.color='green';
done();
},2000)
}
}
})
var vm = new Vue({
el:'#app',
data:{
show:false
},
methods:{
handleClick:function(){
this.show = !this.show;
}
}
})
</script>
把樣式一起封裝進(jìn)來(lái),是比較推薦的方式。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue監(jiān)聽(tīng)路由變化的幾種方式小結(jié)
這篇文章主要介紹了vue監(jiān)聽(tīng)路由變化的幾種方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
vue實(shí)現(xiàn)滾動(dòng)條下滑時(shí)隱藏導(dǎo)航欄,上滑時(shí)顯示導(dǎo)航欄功能
這篇文章主要介紹了vue實(shí)現(xiàn)滾動(dòng)條下滑時(shí)隱藏導(dǎo)航欄,上滑時(shí)顯示導(dǎo)航欄,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-07-07
基于Vue中點(diǎn)擊組件外關(guān)閉組件的實(shí)現(xiàn)方法
下面小編就為大家分享一篇基于Vue中點(diǎn)擊組件外關(guān)閉組件的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
vue-cli項(xiàng)目代理proxyTable配置exclude的方法
今天小編就為大家分享一篇vue-cli項(xiàng)目代理proxyTable配置exclude的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
vue自定義js圖片碎片輪播圖切換效果的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue自定義js圖片碎片輪播圖切換效果的實(shí)現(xiàn)代碼,需要的朋友可以參考下2019-04-04
淺談將three項(xiàng)目遷移至vue項(xiàng)目遇到的問(wèn)題
本文主要介紹了將three項(xiàng)目遷移至vue項(xiàng)目遇到的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
Vue webpack 項(xiàng)目自動(dòng)打包壓縮成zip文件的方法
這篇文章主要介紹了Vue -- webpack 項(xiàng)目自動(dòng)打包壓縮成zip文件的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07
vue中computed下使用箭頭函數(shù)會(huì)報(bào)錯(cuò)問(wèn)題及解決
這篇文章主要介紹了vue中computed下使用箭頭函數(shù)會(huì)報(bào)錯(cuò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07

