Vue2 的动画与过渡
Vue 在插入、更新或者移除 DOM 时,提供多种不同方式的应用过渡的效果
1. 原生 CSS3 的动画
<template>
<h1 class="box leave">Hello,World!!</h1>
</template>
<script>
export default {
name: "HelloWorld",
}
</script>
<style lang="less" scoped>
.box {
margin: 0 auto;
width: 500px;
height: 50px;
background-color: pink;
}
.enter {
animation: hello-box 0.5s linear;
background-color: black;
}
.leave {
animation: hello-box 0.5s linear reverse;
background-color: skyblue;
}
@keyframes hello-box {
from {
transform: translateX(-100%);
}
to {
transform: translateX(0);
}
}
</style>
2. Vue 的动画与过渡
- 利用
v-is
、v-show
显示隐藏元素,Vue 会自动添加过渡效果transition
标签最终并没有形成真实dom
- 使用
<transition></transition>
包裹,transition
标签有一个name
属性,可以用来与过渡动画样式绑定- 默认第一次显示元素的时候是没有动画的,可以添加
appear
属性,使得第一次显示也有动画
(1)通过 name 绑定过渡样式
<template>
<div class="main">
<button @click="isShow = !isShow">显示与隐藏</button>
<transition name="hello" :appear="true">
<h1 v-if="isShow">Hello,World!!</h1>
</transition>
</div>
</template>
<script>
export default {
name: "HelloWorld",
data() {
return {
isShow: true,
}
}
}
</script>
<style scoped>
h1 {
background-color: red;
text-align: center;
}
.hello-enter {
transform: translateX(-100%);
}
.hello-enter-active {
transition: all .5s;
}
.hello-enter-to {
transform: translateX(0);
}
.hello-leave {
transform: translateX(0);
}
.hello-leave-active {
transition: all .5s;
}
.hello-leave-to {
transform: translateX(-100%);
}
</style>
(2)通过 name 绑定动画样式
<template>
<div class="main">
<button @click="isShow = !isShow">显示与隐藏</button>
<transition name="hello" :appear="true">
<h1 v-if="isShow">Hello,World!!</h1>
</transition>
</div>
</template>
<script>
export default {
name: "HelloWorld",
data() {
return {
isShow: true,
}
}
}
</script>
<style scoped>
h1 {
background-color: red;
text-align: center;
}
.hello-enter-active {
animation: hello-box .1s linear;
}
.hello-leave-active {
animation: hello-box .1s linear reverse;
}
@keyframes hello-box {
from {
transform: translateX(-100%);
}
to {
transform: translateX(0);
}
}
</style>
(3)通过 class 绑定过渡样式
<template>
<div class="main">
<button @click="isShow = !isShow">显示与隐藏</button>
<transition
name="hello"
:appear="true"
enter-class="hello-enter"
enter-active-class="hello-enter-active"
enter-to-class="hello-enter-to"
leave-class="hello-leave"
leave-active-class="hello-leave-active"
leave-to-class="hello-leave-to"
>
<h1 v-if="isShow">Hello,World!!</h1>
</transition>
</div>
</template>
<script>
export default {
name: "HelloWorld",
data() {
return {
isShow: true,
}
}
}
</script>
<style scoped>
h1 {
background-color: red;
text-align: center;
}
.hello-enter {
transform: translateX(-100%);
}
.hello-enter-active {
transition: all .5s;
}
.hello-enter-to {
transform: translateX(0);
}
.hello-leave {
transform: translateX(0);
}
.hello-leave-active {
transition: all .5s;
}
.hello-leave-to {
transform: translateX(-100%);
}
</style>
(4)通过 class 绑定动画样式
<template>
<div class="main">
<button @click="isShow = !isShow">显示与隐藏</button>
<transition
name="hello"
:appear="true"
enter-active-class="enter"
leave-active-class="leave"
>
<h1 v-if="isShow">Hello,World!!</h1>
</transition>
</div>
</template>
<script>
export default {
name: "MyTest",
data() {
return {
isShow: true,
}
}
}
</script>
<style scoped>
h1 {
background-color: red;
text-align: center;
}
.enter {
animation: hello-box .5s linear;
}
.leave {
animation: hello-box .5s linear reverse;
}
@keyframes hello-box {
from {
transform: translateX(-100%);
}
to {
transform: translateX(0);
}
}
</style>
3. 多个元素使用过渡
<transition></transition>
标签里面只可以有一个元素- 使用
<transition-group></transition-group>
标签可以包裹多个元素,但是子元素必须配置一个 key,否则报错
(1)使用多个 transition 标签
<template>
<div class="main">
<button @click="isShow = !isShow">显示与隐藏</button>
<transition name="hello" :appear="true">
<h1 v-if="isShow">Hello,World!!</h1>
</transition>
<transition name="hello" :appear="true">
<h1 v-if="!isShow">Hello,World!!</h1>
</transition>
</div>
</template>
<script>
export default {
name: "HelloWorld",
data() {
return {
isShow: true,
}
}
}
</script>
<style scoped>
h1 {
background-color: red;
text-align: center;
}
.hello-enter {
transform: translateX(-100%);
}
.hello-enter-active {
transition: all .5s;
}
.hello-enter-to {
transform: translateX(0);
}
.hello-leave {
transform: translateX(0);
}
.hello-leave-active {
transition: all .5s;
}
.hello-leave-to {
transform: translateX(-100%);
}
</style>
(2)使用 transition-group 标签
<template>
<div class="main">
<button @click="isShow = !isShow">显示与隐藏</button>
<transition-group name="hello" :appear="true">
<h1 v-if="isShow" :key="1">Hello,World!!</h1>
<h1 v-if="!isShow" :key="2">Hello,World!!</h1>
</transition-group>
</div>
</template>
<script>
export default {
name: "HelloWorld",
data() {
return {
isShow: true,
}
}
}
</script>
<style scoped>
h1 {
background-color: red;
text-align: center;
}
.hello-enter {
transform: translateX(-100%);
}
.hello-enter-active {
transition: all .5s;
}
.hello-enter-to {
transform: translateX(0);
}
.hello-leave {
transform: translateX(0);
}
.hello-leave-active {
transition: all .5s;
}
.hello-leave-to {
transform: translateX(-100%);
}
</style>
4. 在 Vue 中使用 Animate.css 动画库
第三方动画库
- 自定义类目使用第三方 CSS 动画库
- 事件的回调函数中使用第三方 JavaScript 动画库直接操作 DOM
使用第三方动画库,这里演示的是:Animate.css | A cross-browser library of CSS animations.
安装
npm install animate.css --save
引入
// 方法一:在 script 标签里面引入
import 'animate.css';
<!-- 方法二:使用 link 标签引入 -->
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"
/>
</head>
使用
- 配置 name 属性
- 使用自定义类目,一般引入第三方动画库,仅配置自定义类名即可
<template>
<div class="main">
<button @click="isShow = !isShow">显示与隐藏</button>
<!--
- 配置 name 属性
- 自定义类名
-->
<transition
name="animate__animated animate__bounce"
:appear="true"
enter-active-class="animate__swing"
leave-active-class="animate__backInUp"
>
<h1 v-if="isShow" style="background-color: red;">Hello,World!!</h1>
</transition>
</div>
</template>
<script>
import 'animate.css';
export default {
name: "MyTest",
data() {
return {
isShow: true,
}
}
}
</script>
<style scoped>
</style>