Vue2過渡標簽transition使用動畫方式
注意:動畫必須使用v-if || v-show配合
1、Vue2配Css3實現(xiàn)
我們需要使用 過渡 標簽 <transition> :
<transition name="hello" appear> <h1 v-show="isShow">你好?。?lt;/h1> </transition> :appear="true" [值需要的是布爾值,所以需要用v-bind綁定] 也可以直接寫 【appear】 appear使用效果是:打開頁面立馬執(zhí)行一次過來的動畫
css3方案一:在樣式style標簽里面設置動畫
【給來和走的樣式的名字定義為 v-enter-active | v-leave-active,設置name的值,需要把v 改成它】
<style> h1 { background-color: orange; } /* 如果過渡標簽加了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解析的時候 <transition> 就沒有了
源碼
<template> <div> <button @click="isShow = !isShow">顯示/隱藏</button> <!--//todo 使用動畫 [ 標簽必須使用v-if||v-show才行 ] //todo 1、使用 過渡 標簽 <transition> 【里面有一個屬性name 標志它的名字】 //todo 2、在樣式style標簽里面設置動畫 //todo 3、給來和走的樣式的名字定義為 v-enter-active | v-leave-active 【設置name的值,需要把v 改成它】 --> <!-- //? 注意 vue解析的時候 <transition> 就沒有了 --> <!-- //todo :appear="true" [值需要的是布爾值,所以需要用v-bind綁定] 也可以直接寫 【appear】 //* appear 使用效果是:打開頁面立馬執(zhí)行一次過來的動畫 --> <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>標簽 里面只能使用一個 DOM 標簽
使用 <transition-group> 可以里面放多個標簽使用動畫
【但是里面加動畫的標簽需要加 唯一標識key 】
<transition-group name="hello" appear> <h1 v-show="isShow" key="1">你好?。?lt;/h1> <h1 v-show="!isShow" key="2">我不好??!</h1> </transition-group>
css3方案2
<style> h1 { background-color: orange; /* transition: 0.5 linear; */ } todo 進入的起點 .hello-enter { transform: translateX(-100%); } todo 進入的終點 .hello-enter-to { transform: translateX(0px); } todo 離開的起點 .hello-leave { transform: translateX(0px); } todo 離開的終點 .hello-leave-to { transform: translateX(-100%); } 簡寫 :進入的起點 就是 離開的終點 .hello-enter, .hello-leave-to { transform: translateX(-100%); } .hello-enter-active, .hello-leave-active { 或者寫在需要加動畫的標簽里面 transition: 0.5 linear; } 簡寫 :進入的終點 就是 離開的起點 .hello-enter-to, .hello-leave { transform: translateX(0px); } </style>
2、vue2配合 animate庫使用動畫
npm install animate.css : 安裝并使用動畫庫
import "animate.css"; 引入該庫
設置
name="animate__animated animate__bounce"
去https://animate.style/里面找動畫,把動畫名稱賦值給enter-active-class 、leave-active-class這兩個屬性
<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、設置 name="animate__animated animate__bounce" --> <!-- // todo 3、去https://animate.style/ 里面找動畫 --> <transition-group name="animate__animated animate__bounce" appear enter-active-class="animate__jackInTheBox" leave-active-class="animate__fadeOut" > <h1 v-show="isShow" key="1">你好??!</h1> </transition-group> </div> </template>
// npm install animate.css : 安裝并使用動畫庫 <script> // todo 1、因為是一個樣式,可以直接引入文件 import "animate.css"; export default { name: "Test", data() { return { isShow: true, }; }, }; </script>
<style> h1 { background-color: orange; /* transition: 0.5 linear; */ } </style>
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
VUE 項目在IE11白屏報錯 SCRIPT1002: 語法錯誤的解決
這篇文章主要介紹了VUE 項目在IE11白屏報錯 SCRIPT1002: 語法錯誤的解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09詳解vue中使用express+fetch獲取本地json文件
本篇文章主要介紹了詳解vue中使用express+fetch獲取本地json文件,非常具有實用價值,需要的朋友可以參考下2017-10-10基于vue-cli配置lib-flexible + rem實現(xiàn)移動端自適應
這篇文章主要介紹了基于vue-cli配置lib-flexible + rem實現(xiàn)移動端自適應,需要的朋友可以參考下2017-12-12vue實現(xiàn)編輯器鍵盤抬起時內(nèi)容跟隨光標距頂位置向上滾動效果
這篇文章主要介紹了vue實現(xiàn)編輯器鍵盤抬起時內(nèi)容跟隨光標距頂位置向上滾動效果,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05