Vue2过渡标签transition使用动画方式
注意:动画必须使用v-if || v-show配合
1、Vue2配Css3实现
我们需要使用 过渡 标签 <transition> :
<transition name="hello" appear>
<h1 v-show="isShow">你好啊!</h1>
</transition>
:appear="true" [值需要的是布尔值,所以需要用v-bind绑定] 也可以直接写 【appear】
appear使用效果是:打开页面立马执行一次过来的动画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 使用效果是:打开页面立马执行一次过来的动画 -->
<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">你好啊!</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">你好啊!</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>总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
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实现移动端自适应
这篇文章主要介绍了基于vue-cli配置lib-flexible + rem实现移动端自适应,需要的朋友可以参考下2017-12-12


最新评论