Vue中動(dòng)畫(huà)與過(guò)渡的使用教程
前言
本篇博客將會(huì)介紹如何在Vue中使用動(dòng)畫(huà)效果。
一、回憶css3中的動(dòng)畫(huà)
定義一個(gè)動(dòng)畫(huà):
定義一個(gè)動(dòng)畫(huà)名為atguigu
@keyframes atguigu {
from {
transform: translateX(-100%);
}
to {
transform: translateX(0px);
}
}
使用動(dòng)畫(huà)
h1 {
text-align: center;
background-color: rgba(0, 217, 255, 0.897);
}
將動(dòng)畫(huà)使用到come類中
.come {
animation: atguigu 0.5s linear;
}
將動(dòng)畫(huà)atguigu的相反使用到to類中
.to {
animation: atguigu 0.5s linear reverse;
}
animation: name duration timing-function delay iteration-count direction fill-mode;

二、Vue中單標(biāo)簽使用動(dòng)畫(huà)
vue中定義動(dòng)畫(huà)使用,需要將響應(yīng)標(biāo)簽放入標(biāo)簽 <transition></transition>中
若有多個(gè)元素需要過(guò)度,則需要使用:<transition-group>,且每個(gè)元素都要指定key值。
1.默認(rèn)使用方法
這種方法只適用于一個(gè)插件只有一個(gè)動(dòng)畫(huà)效果,因?yàn)闆](méi)有辦法對(duì)動(dòng)畫(huà)進(jìn)行區(qū)分
元素進(jìn)入的樣式:
- v-enter:進(jìn)入的起點(diǎn)
- v-enter-active:進(jìn)入過(guò)程中
- v-enter-to:進(jìn)入的終點(diǎn)
元素離開(kāi)的樣式:
- v-leave:離開(kāi)的起點(diǎn)
- v-leave-active:離開(kāi)過(guò)程中
- v-leave-to:離開(kāi)的終點(diǎn)
可以結(jié)合以下一個(gè)實(shí)例使用:
<template>
<div>
<button @click="isShow = !isShow">顯示/隱藏</button>
//appear 屬性加上會(huì)在頁(yè)面加載時(shí)執(zhí)行動(dòng)畫(huà)
<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)簽時(shí)激活
.v-enter-active{
animation: atguigu 0.5s linear;
}
//不展示標(biāo)簽時(shí)激活
.v-leave-active{
animation: atguigu 0.5s linear reverse;
}
@keyframes atguigu {
from{
transform: translateX(-100%);
}
to{
transform: translateX(0px);
}
}
</style>
2.自定義使用方法
這種方法較為靈活,一個(gè)插件可以定義多個(gè)動(dòng)畫(huà),并用定義的名字進(jìn)行區(qū)分,用法如下:
<template>
<div>
<button @click="isShow = !isShow">顯示/隱藏</button>
//給標(biāo)簽指定一個(gè)名字
<transition name="hello" 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;
}
//這里的寫(xiě)法有所改變,應(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)動(dòng)畫(huà)效果
上面介紹到的transition只能用于包裹一個(gè)標(biāo)簽,如果包裹多個(gè)標(biāo)簽的話就會(huì)報(bào)錯(cuò)。如果想要包裹多個(gè)標(biāo)簽可以使用transition-group。除了使用定義的動(dòng)畫(huà),還可以使用過(guò)渡效果實(shí)現(xiàn)動(dòng)畫(huà)。
具體的使用方法如下:
<template>
<div>
<button @click="isShow = !isShow">顯示/隱藏</button>
//里面的兩個(gè)h1均由動(dòng)畫(huà)效果
<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 會(huì)在指定的時(shí)期,將相應(yīng)的動(dòng)畫(huà)效果展示出來(lái),我們只用這樣寫(xiě)即可。
/* 進(jìn)入的起點(diǎn)、離開(kāi)的終點(diǎn) */
.hello-enter,.hello-leave-to{
transform: translateX(-100%);
}
/* 進(jìn)入的終點(diǎn)、離開(kāi)的起點(diǎn) */
.hello-enter-to,.hello-leave{
transform: translateX(0);
}
.hello-enter-active,.hello-leave-active{
transition: 0.5s linear;
}
</style>
四、使用第三方動(dòng)畫(huà)
市面上有許多優(yōu)秀的動(dòng)畫(huà)庫(kù),我們?cè)谑褂玫臅r(shí)候只需進(jìn)行一些簡(jiǎn)單的配置就可以使用。
下面有一個(gè)案例,是使用的animate.css動(dòng)畫(huà)庫(kù)可以參考一下:

<template>
<div>
<button @click="isShow = !isShow">顯示/隱藏</button>
<transition-group
appear
//下面三行為官網(wǎng)給出的配置
name="animate__animated animate__bounce"
//這里就是顯示組件跟隱藏組件時(shí)的動(dòng)畫(huà)
//等號(hào)后面的東西直接去官網(wǎng)找自己想要的效果然后把名稱復(fù)制上去即可
enter-active-class="animate__swing"
leave-active-class="animate__backOutUp"
>
<h1 v-show="!isShow" key="1">你好?。?lt;/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中動(dòng)畫(huà)與過(guò)渡的使用教程的文章就介紹到這了,更多相關(guān)Vue動(dòng)畫(huà)與過(guò)渡內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用vue ant design分頁(yè)以及表格分頁(yè)改為中文問(wèn)題
這篇文章主要介紹了使用vue ant design分頁(yè)以及表格分頁(yè)改為中文問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2023-04-04
Pinia入門(mén)學(xué)習(xí)之實(shí)現(xiàn)簡(jiǎn)單的用戶狀態(tài)管理
Vue3雖然相對(duì)于Vue2很多東西都變了,但是核心的東西還是沒(méi)有變,比如說(shuō)狀態(tài)管理、路由等,再Vue3中尤大神推薦我們使用pinia來(lái)實(shí)現(xiàn)狀態(tài)管理,他也說(shuō)pinia就是Vuex的新版本,這篇文章主要給大家介紹了關(guān)于Pinia入門(mén)學(xué)習(xí)之實(shí)現(xiàn)簡(jiǎn)單的用戶狀態(tài)管理的相關(guān)資料,需要的朋友可以參考下2022-11-11
vue-以文件流-blob-的形式-下載-導(dǎo)出文件操作
這篇文章主要介紹了vue-以文件流-blob-的形式-下載-導(dǎo)出文件操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
一文快速學(xué)會(huì)阻止事件冒泡的4種方法(原生js阻止,vue中使用修飾符阻止)
冒泡就是事件開(kāi)始是由最具體的元素接收,然后逐層向上級(jí)傳播到較為不具體的元素,這篇文章主要給大家介紹了關(guān)于阻止事件冒泡的4種方法,文中介紹的方法分別是原生js阻止以及vue中使用修飾符阻止的相關(guān)資料,需要的朋友可以參考下2023-12-12
Vue+Element樹(shù)形表格實(shí)現(xiàn)拖拽排序示例
本文主要介紹了Vue+Element樹(shù)形表格實(shí)現(xiàn)拖拽排序示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08

