Vue2過(guò)渡標(biāo)簽transition使用動(dòng)畫(huà)方式
注意:動(dòng)畫(huà)必須使用v-if || v-show配合
1、Vue2配Css3實(shí)現(xiàn)
我們需要使用 過(guò)渡 標(biāo)簽 <transition> :
<transition name="hello" appear>
<h1 v-show="isShow">你好啊!</h1>
</transition>
:appear="true" [值需要的是布爾值,所以需要用v-bind綁定] 也可以直接寫(xiě) 【appear】
appear使用效果是:打開(kāi)頁(yè)面立馬執(zhí)行一次過(guò)來(lái)的動(dòng)畫(huà)css3方案一:在樣式style標(biāo)簽里面設(shè)置動(dòng)畫(huà)
【給來(lái)和走的樣式的名字定義為 v-enter-active | v-leave-active,設(shè)置name的值,需要把v 改成它】
<style>
h1 {
background-color: orange;
}
/* 如果過(guò)渡標(biāo)簽加了name屬性,下面的v需要改為name的值
.v-enter-active {
animation: gbase 1s;
}
.v-leave-active {
animation: gbase 1s reverse;
}
*/
.hello-enter-active {
animation: gbase 1s;
}
.hello-leave-active {
animation: gbase 1s reverse;
}
@keyframes gbase {
from {
transform: translateX(-100%);
}
to {
transform: translateX(0px);
}
}
</style>注意:vue解析的時(shí)候 <transition> 就沒(méi)有了
源碼
<template>
<div>
<button @click="isShow = !isShow">顯示/隱藏</button>
<!--//todo 使用動(dòng)畫(huà) [ 標(biāo)簽必須使用v-if||v-show才行 ]
//todo 1、使用 過(guò)渡 標(biāo)簽 <transition> 【里面有一個(gè)屬性name 標(biāo)志它的名字】
//todo 2、在樣式style標(biāo)簽里面設(shè)置動(dòng)畫(huà)
//todo 3、給來(lái)和走的樣式的名字定義為 v-enter-active | v-leave-active 【設(shè)置name的值,需要把v 改成它】
-->
<!-- //? 注意 vue解析的時(shí)候 <transition> 就沒(méi)有了 -->
<!-- //todo :appear="true" [值需要的是布爾值,所以需要用v-bind綁定] 也可以直接寫(xiě) 【appear】
//* appear 使用效果是:打開(kāi)頁(yè)面立馬執(zhí)行一次過(guò)來(lái)的動(dòng)畫(huà) -->
<transition name="hello" appear>
<h1 v-show="isShow">你好??!</h1>
</transition>
</div>
</template><script>
export default {
name: "Test",
data() {
return {
isShow: true,
};
},
};
</script><style>
h1 {
background-color: orange;
}
.hello-enter-active {
animation: gbase 1s;
}
.hello-leave-active {
animation: gbase 1s reverse;
}
@keyframes gbase {
from {
transform: translateX(-100%);
}
to {
transform: translateX(0px);
}
}
</style>其他屬性樣式
<transition>標(biāo)簽 里面只能使用一個(gè) DOM 標(biāo)簽
使用 <transition-group> 可以里面放多個(gè)標(biāo)簽使用動(dòng)畫(huà)
【但是里面加動(dòng)畫(huà)的標(biāo)簽需要加 唯一標(biāo)識(shí)key 】
<transition-group name="hello" appear>
<h1 v-show="isShow" key="1">你好??!</h1>
<h1 v-show="!isShow" key="2">我不好??!</h1>
</transition-group>css3方案2
<style>
h1 {
background-color: orange;
/* transition: 0.5 linear; */
}
todo 進(jìn)入的起點(diǎn)
.hello-enter {
transform: translateX(-100%);
}
todo 進(jìn)入的終點(diǎn)
.hello-enter-to {
transform: translateX(0px);
}
todo 離開(kāi)的起點(diǎn)
.hello-leave {
transform: translateX(0px);
}
todo 離開(kāi)的終點(diǎn)
.hello-leave-to {
transform: translateX(-100%);
}
簡(jiǎn)寫(xiě) :進(jìn)入的起點(diǎn) 就是 離開(kāi)的終點(diǎn)
.hello-enter,
.hello-leave-to {
transform: translateX(-100%);
}
.hello-enter-active,
.hello-leave-active {
或者寫(xiě)在需要加動(dòng)畫(huà)的標(biāo)簽里面
transition: 0.5 linear;
}
簡(jiǎn)寫(xiě) :進(jìn)入的終點(diǎn) 就是 離開(kāi)的起點(diǎn)
.hello-enter-to,
.hello-leave {
transform: translateX(0px);
}
</style>2、vue2配合 animate庫(kù)使用動(dòng)畫(huà)
npm install animate.css : 安裝并使用動(dòng)畫(huà)庫(kù)
import "animate.css"; 引入該庫(kù)
設(shè)置
name="animate__animated animate__bounce"
去https://animate.style/里面找動(dòng)畫(huà),把動(dòng)畫(huà)名稱(chēng)賦值給enter-active-class 、leave-active-class這兩個(gè)屬性

<transition-group
name="animate__animated animate__bounce"
appear
enter-active-class="animate__jackInTheBox"
leave-active-class="animate__fadeOut"
>
<h1 v-show="isShow" key="1">你好?。?lt;/h1>
</transition-group>源碼
<template>
<div>
<button @click="isShow = !isShow">顯示/隱藏</button>
<!-- // todo 2、設(shè)置 name="animate__animated animate__bounce" -->
<!-- // todo 3、去https://animate.style/ 里面找動(dòng)畫(huà) -->
<transition-group
name="animate__animated animate__bounce"
appear
enter-active-class="animate__jackInTheBox"
leave-active-class="animate__fadeOut"
>
<h1 v-show="isShow" key="1">你好?。?lt;/h1>
</transition-group>
</div>
</template>// npm install animate.css : 安裝并使用動(dòng)畫(huà)庫(kù)
<script>
// todo 1、因?yàn)槭且粋€(gè)樣式,可以直接引入文件
import "animate.css";
export default {
name: "Test",
data() {
return {
isShow: true,
};
},
};
</script><style>
h1 {
background-color: orange;
/* transition: 0.5 linear; */
}
</style>總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vite搭建vue2項(xiàng)目的實(shí)戰(zhàn)過(guò)程
自從體驗(yàn)了一下vite之后,真的太快了,然而對(duì)vue3還不是很熟練,就想著在vue2的項(xiàng)目中使用以下vite,下面這篇文章主要給大家介紹了關(guān)于vite搭建vue2項(xiàng)目的相關(guān)資料,需要的朋友可以參考下2022-11-11
vue實(shí)現(xiàn)上傳圖片添加水印(升級(jí)版)
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)上傳圖片添加水印的升級(jí)版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
VUE 項(xiàng)目在IE11白屏報(bào)錯(cuò) SCRIPT1002: 語(yǔ)法錯(cuò)誤的解決
這篇文章主要介紹了VUE 項(xiàng)目在IE11白屏報(bào)錯(cuò) SCRIPT1002: 語(yǔ)法錯(cuò)誤的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
vue 解決移動(dòng)端彈出鍵盤(pán)導(dǎo)致頁(yè)面fixed布局錯(cuò)亂的問(wèn)題
今天小編就為大家分享一篇vue 解決移動(dòng)端彈出鍵盤(pán)導(dǎo)致頁(yè)面fixed布局錯(cuò)亂的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
詳解vue中使用express+fetch獲取本地json文件
本篇文章主要介紹了詳解vue中使用express+fetch獲取本地json文件,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10
VueJS組件之間通過(guò)props交互及驗(yàn)證的方式
本篇文章主要介紹了VueJS組件之間通過(guò)props交互及驗(yàn)證的方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-09-09
基于vue-cli配置lib-flexible + rem實(shí)現(xiàn)移動(dòng)端自適應(yīng)
這篇文章主要介紹了基于vue-cli配置lib-flexible + rem實(shí)現(xiàn)移動(dòng)端自適應(yīng),需要的朋友可以參考下2017-12-12
vue2+elementui進(jìn)行hover提示的使用
本文主要介紹了vue2+elementui進(jìn)行hover提示的使用,主要分為外部和內(nèi)部,具有一定的參考價(jià)值,感興趣的可以了解一下2021-11-11
vue實(shí)現(xiàn)編輯器鍵盤(pán)抬起時(shí)內(nèi)容跟隨光標(biāo)距頂位置向上滾動(dòng)效果
這篇文章主要介紹了vue實(shí)現(xiàn)編輯器鍵盤(pán)抬起時(shí)內(nèi)容跟隨光標(biāo)距頂位置向上滾動(dòng)效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05

