Vue中動畫與過渡的使用教程
前言
本篇博客將會介紹如何在Vue中使用動畫效果。
一、回憶css3中的動畫
定義一個動畫:
定義一個動畫名為atguigu @keyframes atguigu { from { transform: translateX(-100%); } to { transform: translateX(0px); } }
使用動畫
h1 { text-align: center; background-color: rgba(0, 217, 255, 0.897); } 將動畫使用到come類中 .come { animation: atguigu 0.5s linear; } 將動畫atguigu的相反使用到to類中 .to { animation: atguigu 0.5s linear reverse; }
animation: name duration timing-function delay iteration-count direction fill-mode;
二、Vue中單標(biāo)簽使用動畫
vue中定義動畫使用,需要將響應(yīng)標(biāo)簽放入標(biāo)簽 <transition></transition>中
若有多個元素需要過度,則需要使用:<transition-group>
,且每個元素都要指定key
值。
1.默認(rèn)使用方法
這種方法只適用于一個插件只有一個動畫效果,因?yàn)闆]有辦法對動畫進(jìn)行區(qū)分
元素進(jìn)入的樣式:
- v-enter:進(jìn)入的起點(diǎn)
- v-enter-active:進(jìn)入過程中
- v-enter-to:進(jìn)入的終點(diǎn)
元素離開的樣式:
- v-leave:離開的起點(diǎn)
- v-leave-active:離開過程中
- v-leave-to:離開的終點(diǎn)
可以結(jié)合以下一個實(shí)例使用:
<template> <div> <button @click="isShow = !isShow">顯示/隱藏</button> //appear 屬性加上會在頁面加載時執(zhí)行動畫 <transition appear> <h1 v-show="isShow">你好??!</h1> </transition> </div> </template> <script> export default { name:'Hello', data() { return { isShow:true } }, } </script> <style scoped> h1{ background-color: orange; } //展示標(biāo)簽時激活 .v-enter-active{ animation: atguigu 0.5s linear; } //不展示標(biāo)簽時激活 .v-leave-active{ animation: atguigu 0.5s linear reverse; } @keyframes atguigu { from{ transform: translateX(-100%); } to{ transform: translateX(0px); } } </style>
2.自定義使用方法
這種方法較為靈活,一個插件可以定義多個動畫,并用定義的名字進(jìn)行區(qū)分,用法如下:
<template> <div> <button @click="isShow = !isShow">顯示/隱藏</button> //給標(biāo)簽指定一個名字 <transition name="hello" appear> <h1 v-show="isShow">你好?。?lt;/h1> </transition> </div> </template> <script> export default { name:'Hello', data() { return { isShow:true } }, } </script> <style scoped> h1{ background-color: orange; } //這里的寫法有所改變,應(yīng)為.name-enter-activate..... .hello-enter-active{ animation: atguigu 0.5s linear; } .hello-leave-active{ animation: atguigu 0.5s linear reverse; } @keyframes atguigu { from{ transform: translateX(-100%); } to{ transform: translateX(0px); } } </style>
三、Vue中多標(biāo)簽實(shí)現(xiàn)動畫效果
上面介紹到的transition只能用于包裹一個標(biāo)簽,如果包裹多個標(biāo)簽的話就會報(bào)錯。如果想要包裹多個標(biāo)簽可以使用transition-group。除了使用定義的動畫,還可以使用過渡效果實(shí)現(xiàn)動畫。
具體的使用方法如下:
<template> <div> <button @click="isShow = !isShow">顯示/隱藏</button> //里面的兩個h1均由動畫效果 <transition-group name="hello" appear> <h1 v-show="!isShow" key="1">你好??!</h1> <h1 v-show="isShow" key="2">尚硅谷!</h1> </transition-group> </div> </template> <script> export default { name:'Test', data() { return { isShow:true } }, } </script> <style scoped> h1{ background-color: orange; } //Vue 會在指定的時期,將相應(yīng)的動畫效果展示出來,我們只用這樣寫即可。 /* 進(jìn)入的起點(diǎn)、離開的終點(diǎn) */ .hello-enter,.hello-leave-to{ transform: translateX(-100%); } /* 進(jìn)入的終點(diǎn)、離開的起點(diǎn) */ .hello-enter-to,.hello-leave{ transform: translateX(0); } .hello-enter-active,.hello-leave-active{ transition: 0.5s linear; } </style>
四、使用第三方動畫
市面上有許多優(yōu)秀的動畫庫,我們在使用的時候只需進(jìn)行一些簡單的配置就可以使用。
下面有一個案例,是使用的animate.css動畫庫可以參考一下:
<template> <div> <button @click="isShow = !isShow">顯示/隱藏</button> <transition-group appear //下面三行為官網(wǎng)給出的配置 name="animate__animated animate__bounce" //這里就是顯示組件跟隱藏組件時的動畫 //等號后面的東西直接去官網(wǎng)找自己想要的效果然后把名稱復(fù)制上去即可 enter-active-class="animate__swing" leave-active-class="animate__backOutUp" > <h1 v-show="!isShow" key="1">你好啊!</h1> <h1 v-show="isShow" key="2">尚硅谷!</h1> </transition-group> </div> </template> <script> import 'animate.css' export default { name:'Test', data() { return { isShow:true } }, } </script> <style scoped> h1{ background-color: orange; } </style>
到此這篇關(guān)于Vue中動畫與過渡的使用教程的文章就介紹到這了,更多相關(guān)Vue動畫與過渡內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue實(shí)現(xiàn)實(shí)時監(jiān)聽頁面寬度高度變化
這篇文章主要為大家詳細(xì)介紹了Vue如何實(shí)現(xiàn)實(shí)時監(jiān)聽頁面寬度高度變化,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03解決Vue中的生命周期beforeDestory不觸發(fā)的問題
這篇文章主要介紹了解決Vue中的生命周期beforeDestory不觸發(fā)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07element-ui tree結(jié)構(gòu)實(shí)現(xiàn)增刪改自定義功能代碼
這篇文章主要介紹了element-ui tree結(jié)構(gòu)實(shí)現(xiàn)增刪改自定義功能代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08使用Element進(jìn)行前端開發(fā)的詳細(xì)圖文教程
眾所周知Element是一套Vue.js后臺組件庫,它能夠幫助你更輕松更快速地開發(fā)后臺項(xiàng)目,下面這篇文章主要給大家介紹了關(guān)于使用Element進(jìn)行前端開發(fā)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11