Vue過渡效果之CSS過渡詳解(結(jié)合transition,animation,animate.css)
以一個toggle按鈕控制p元素顯隱為例,如果不使用過渡效果,則如下所示
<div id="demo"> <button v-on:click="show = !show">Toggle</button> <p v-if="show">藍色理想</p> </div> <script> new Vue({ el: '#demo', data: { show: true } }) </script>
如果要為此加入過渡效果,則需要使用過渡組件transition
過渡組件
Vue提供了transition的封裝組件,下面代碼中,該過渡組件的名稱為'fade'
<transition name="fade"> <p v-if="show">藍色理想</p> </transition>
當(dāng)插入或刪除包含在transition組件中的元素時,Vue會自動嗅探目標(biāo)元素是否應(yīng)用了 CSS 過渡或動畫,如果是,在恰當(dāng)?shù)臅r機添加/刪除 CSS 類名
過渡類名
總共有6個(CSS)類名在enter/leave的過渡中切換
v-enter
定義進入過渡的開始狀態(tài)。在元素被插入時生效,在下一個幀移除
v-enter-active
定義過渡的狀態(tài)。在元素整個過渡過程中作用,在元素被插入時生效,在 transition 或 animation 完成之后移除。 這個類可以被用來定義過渡的過程時間,延遲和曲線函數(shù)
v-enter-to
定義進入過渡的結(jié)束狀態(tài)。在元素被插入一幀后生效(與此同時 v-enter 被刪除),在 transition 或 animation 完成之后移除
v-leave
定義離開過渡的開始狀態(tài)。在離開過渡被觸發(fā)時生效,在下一個幀移除
v-leave-active
定義過渡的狀態(tài)。在元素整個過渡過程中作用,在離開過渡被觸發(fā)后立即生效,在 transition 或 animation 完成之后移除。 這個類可以被用來定義過渡的過程時間,延遲和曲線函數(shù)
v-leave-to
定義離開過渡的結(jié)束狀態(tài)。在離開過渡被觸發(fā)一幀后生效(與此同時 v-leave 被刪除),在 transition 或 animation 完成之后移除
對于這些在 enter/leave 過渡中切換的類名,v- 是這些類名的前綴,表示過渡組件的名稱。比如,如果使用 <transition name="my-transition"> ,則 v-enter替換為 my-transition-enter
transition
常用的Vue過渡效果都是使用CSS過渡transition,下面增加一個enter時透明度變化,leave時位移變化的效果
<style> .fade-enter{ opacity:0; } .fade-enter-active{ transition:opacity .5s; } .fade-leave-active{ transition:transform .5s; } .fade-leave-to{ transform:translateX(10px); } </style> <div id="demo"> <button v-on:click="show = !show">Toggle</button> <transition name="fade"> <p v-if="show">藍色理想</p> </transition> </div> <script> new Vue({ el: '#demo', data: { show: true } }) </script>
animation
CSS動畫animation用法同CSS過渡transition,區(qū)別是在動畫中 v-enter 類名在節(jié)點插入 DOM 后不會立即刪除,而是在 animationend 事件觸發(fā)時刪除
下面例子中,在元素enter和leave時都增加縮放scale效果
<style> .bounce-enter-active{ animation:bounce-in .5s; } .bounce-leave-active{ animation:bounce-in .5s reverse; } @keyframes bounce-in{ 0%{transform:scale(0);} 50%{transform:scale(1.5);} 100%{transform:scale(1);} } </style> <div id="demo"> <button v-on:click="show = !show">Toggle</button> <transition name="bounce"> <p v-if="show">藍色理想</p> </transition> </div> <script> new Vue({ el: '#demo', data: { show: true } }) </script>
同時使用
Vue 為了知道過渡的完成,必須設(shè)置相應(yīng)的事件監(jiān)聽器。它可以是 transitionend 或 animationend ,這取決于給元素應(yīng)用的 CSS 規(guī)則。如果使用其中任何一種,Vue 能自動識別類型并設(shè)置監(jiān)聽。
但是,在一些場景中,需要給同一個元素同時設(shè)置兩種過渡動效,比如 animation 很快的被觸發(fā)并完成了,而 transition 效果還沒結(jié)束。在這種情況中,就需要使用 type 特性并設(shè)置 animation 或 transition 來明確聲明需要 Vue 監(jiān)聽的類型
<style> .fade-enter,.fade-leave-to{ opacity:0; } .fade-enter-active,.fade-leave-active{ transition:opacity 1s; animation:bounce-in 5s; } @keyframes bounce-in{ 0%{transform:scale(0);} 50%{transform:scale(1.5);} 100%{transform:scale(1);} } </style> <div id="demo"> <button v-on:click="show = !show">Toggle</button> <transition name="fade" type="transition"> <p v-if="show">藍色理想</p> </transition> </div> <script> new Vue({ el: '#demo', data: { show: true, }, }) </script>
自定義類名
可以通過以下特性來自定義過渡類名
enter-class
enter-active-class
enter-to-class
leave-class
leave-active-class
leave-to-class
自定義類名的優(yōu)先級高于普通的類名,這對于Vue的過渡系統(tǒng)和其他第三方CSS動畫庫,如 Animate.css 結(jié)合使用十分有用
<link rel="stylesheet" rel="external nofollow" > <div id="example"> <button @click="show = !show"> Toggle render </button> <transition name="xxx" enter-active-class="animated tada" leave-active-class="animated bounceOutRight"> <p v-if="show">藍色理想</p> </transition> </div> <script src="https://www.jb51.com/vue"></script> <script> new Vue({ el: '#example', data: { show: true } }) </script>
初始渲染過渡
可以通過 appear 特性設(shè)置節(jié)點的在初始渲染的過渡
<transition appear> <!-- ... --> </transition>
這里默認和進入和離開過渡一樣,同樣也可以自定義 CSS 類名
<transition appear appear-class="custom-appear-class" appear-to-class="custom-appear-to-class" appear-active-class="custom-appear-active-class" > <!-- ... --> </transition>
下面是一個例子
<style> .custom-appear-class{ opacity:0; background-color:pink; transform:translateX(100px); } .custom-appear-active-class{ transition: 2s; } </style> <div id="demo"> <button @click="reset">還原</button> <transition appear appear-class="custom-appear-class" appear-to-class="custom-appear-to-class" appear-active-class="custom-appear-active-class"> <p>藍色理想</p> </transition> </div> <script> new Vue({ el: '#demo', methods:{ reset(){ history.go(); } } }) </script>
過渡時間
在很多情況下,Vue可以自動得出過渡效果的完成時機。默認情況下,Vue會等待其在過渡效果的根元素的第一個 transitionend 或 animationend 事件。然而也可以不這樣設(shè)定——比如,可以擁有一個精心編排的一序列過渡效果,其中一些嵌套的內(nèi)部元素相比于過渡效果的根元素有延遲的或更長的過渡效果
在這種情況下可以用<transition>組件上的duration屬性定制一個顯性的過渡效果持續(xù)時間 (以毫秒計)
下面的代碼意味著元素在進入enter和離開leave時,持續(xù)時間都為1s,而無論在樣式中它們的設(shè)置值為多少
<transition :duration="1000">...</transition>
也可以分別定制進入和移出的持續(xù)時間
<transition :duration="{ enter: 500, leave: 800 }">...</transition>
比如,下面的代碼中,進入和移出的效果都為animate.css里面的shake效果,但持續(xù)時間分別是0.5s和1s
<div id="demo"> <button v-on:click="show = !show">Toggle</button> <transition :duration="{ enter: 500, leave: 1000 }" name="xxx" enter-active-class="animated shake" leave-active-class="animated shake"> <p v-if="show">小火柴的藍色理想</p> </transition> </div> <script> new Vue({ el: '#demo', data: { show: true } }) </script>
過渡條件
一般地,在Vue中滿足下列任意一個過渡條件,即可添加過渡效果
條件渲染(使用v-if)
常見的條件是使用條件渲染,使用v-if
<style> .fade-enter,.fade-leave-to{ opacity:0; } .fade-enter-active,.fade-leave-active{ transition:opacity 1s; } </style> <div id="demo"> <button v-on:click="show = !show">Toggle</button> <transition name="fade"> <p v-if="show">藍色理想</p> </transition> </div> <script> new Vue({ el: '#demo', data: { show: true } }) </script>
條件展示(使用v-show)
使用條件展示,即使用v-show時,也可以添加過渡效果
<div id="demo"> <button v-on:click="show = !show">Toggle</button> <transition name="fade"> <p v-show="show">藍色理想</p> </transition> </div>
動態(tài)組件
使用is屬性實現(xiàn)的動態(tài)組件,可以添加過渡效果
<div id="demo"> <button v-on:click="show = !show">Toggle</button> <transition name="fade"> <component :is="view"></component> </transition> </div> <script> new Vue({ el: '#demo', components:{ 'home':{template:'<div>藍色理想</div>'} }, data: { show: true, }, computed:{ view(){ return this.show ? 'home' : ''; } } })
更多關(guān)于Vue動畫效果的文章請點擊下面的相關(guān)鏈接
相關(guān)文章
vue登錄頁實現(xiàn)使用cookie記住7天密碼功能的方法
這篇文章主要介紹了vue登錄頁實現(xiàn)使用cookie記住7天密碼功能的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Vue3中Provide?/?Inject的實現(xiàn)原理分享
provide和inject主要為高階插件/組件庫提供用例,這篇文章主要給大家介紹了關(guān)于Vue3中Provide?/?Inject的實現(xiàn)原理,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-02-02解決element?ui?cascader?動態(tài)加載回顯問題
這篇文章主要介紹了element?ui?cascader?動態(tài)加載回顯問題解決方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08vue2使用element-ui,el-table不顯示,用npm安裝方式
這篇文章主要介紹了vue2使用element-ui,el-table不顯示,用npm安裝方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07