概述
Vue 在插入、更新或者移除 DOM 时,提供多种不同方式的应用过渡效果。 包括以下工具:
- 在 CSS 过渡和动画中自动应用 class
- 可以配合使用第三方 CSS 动画库,如 Animate.css
- 在过渡钩子函数中使用 JavaScript 直接操作 DOM
- 可以配合使用第三方 JavaScript 动画库,如 Velocity.js
上一篇说明了使用「过渡类名」来实现动画效果,但是每个动画都要自己去写的话,其实是一个挺麻烦的事情,本篇章来说明使用第三方css动画库「Animate.css」来实现动画效果。
Animate.css 库介绍
简介
animate.css 是一个来自国外的 CSS3 动画库,它预设了抖动(shake)、闪烁(flash)、弹跳(bounce)、翻转(flip)、旋转(rotateIn/rotateOut)、淡入淡出(fadeIn/fadeOut)等多达 60 多种动画效果,几乎包含了所有常见的动画效果。
虽然借助 animate.css 能够很方便、快速的制作 CSS3 动画效果,但还是建议看看 animate.css 的代码,也许你能从中学到一些东西。
相关网址
animate.css中文网:http://www.animate.net.cn/
![9233d58fd61f9bebe4d881db780b0863.png](https://i-blog.csdnimg.cn/blog_migrate/2e7fdb62904d8a77814e78dc0a6e3715.png)
在进入Animate中文网之后,可以查看部分的在线演示。
![e6c9e5492a86044d5ecb9e78d9a3d554.png](https://i-blog.csdnimg.cn/blog_migrate/e1d2fa8f76cc960b97b036d0fe2be1e8.png)
可以看到介绍,使用animate库非常简单,下面来看看如果引入使用。
下载animate库
![2691ab32fd4a717717d4e4ef91960868.png](https://i-blog.csdnimg.cn/blog_migrate/5fd35b9716ab4c722bacd9ea77b5b7a2.png)
下载地址:https://daneden.github.io/animate.css/
![5c941245b0ee7a35a947703e469a7840.png](https://i-blog.csdnimg.cn/blog_migrate/93a1966da4ac0ac8ad5f75c2a5a8ae7c.png)
直接点击这个地址下载的话,我目前访问页面失败。然后我又默默去Github中的release页面来下载。
https://github.com/daneden/animate.css/releases
![e01ee1e2ca43d7de31f3de0248617f49.png](https://i-blog.csdnimg.cn/blog_migrate/b85d99d8bfe8411337b09c420b1c965b.png)
解压下载的zip包,可以看到animate.css的相关文件:
![2217179fae25e713bbb8ccb535f198b4.png](https://i-blog.csdnimg.cn/blog_migrate/5510e2240dd299ce8063b2497bd89b12.png)
在项目中开发中,只需要导入这个animate.min.css
压缩文件即可。
使用示例 1
span style="line-height: 26px;">html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<link rel="stylesheet" href="lib/animate.css-3.7.2/animate.min.css">
head>
<body>
<div class="animated fadeInUp">
<h1>hello worldh1>
div>
body>
html>
浏览器执行效果,如下:
![9a75aa2920e0bfa66a5e7d09a1ad1ab6.png](https://i-blog.csdnimg.cn/blog_migrate/9c7d9d73099dccd126784e7a5bd17436.png)
上面的示例给元素加上 class 后,刷新页面,就能看到动画效果了。animated 类似于全局变量,它定义了动画的持续时间;bounce 是动画具体的动画效果的名称,你可以选择任意的效果。
如果动画是无限播放的,可以添加 class infinite,如下:
hello world
hello world
使用示例 2
你也可以通过 JavaScript 或 jQuery 给元素添加这些 class,比如:
$(function(){
$('#dowebok').addClass('animated bounce');
});
有些动画效果最后会让元素不可见,比如淡出、向左滑动等等,可能你又需要将 class 删除,比如:
$(function(){
$('#dowebok').addClass('animated bounce');
setTimeout(function(){
$('#dowebok').removeClass('bounce');
}, 1000);
});
animate.css 的默认设置也许有些时候并不是我们想要的,所以你可以重新设置,比如:
#dowebok {
animate-duration: 2s; //动画持续时间
animate-delay: 1s; //动画延迟时间
animate-iteration-count: 2; //动画执行次数
}
注意添加浏览器前缀。
-ms- 兼容IE浏览器
-moz- 兼容firefox
-o- 兼容opera
-webkit- 兼容chrome 和 safari
完整示例代码如下:
span style="line-height: 26px;">html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<link rel="stylesheet" href="lib/animate.css-3.7.2/animate.min.css">
<script src="lib/jquery/jquery-3.4.1.min.js">script>
<script>
$(function(){
$('#dowebok').addClass('animated bounce');
setTimeout(function(){
$('#dowebok').removeClass('bounce');
}, 1000);
});script>
head>
<body>
<div id="dowebok">
<h1>Bounceh1>
div>
body>
html>
浏览器显示如下:
![95af34a25f0717cc24ed3b979317a88b.png](https://i-blog.csdnimg.cn/blog_migrate/4f1a80ebc584364551d1d562fd3425b9.png)
更多的动画效果,可以根据在线演示来查看,如下:
https://daneden.github.io/animate.css/
![3b4fee621fd4bce71e5909e2b8dcd4ce.png](https://i-blog.csdnimg.cn/blog_migrate/564ad05fffaf61d344cc033b4186d099.png)
下面来看看如何在Vue框架中应用。
在Vue框架中应用animate.css库
使用enter-active-class
和leave-active-class
应用css动画
span style="line-height: 26px;">html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="lib/vue.js">script>
<link rel="stylesheet" href="lib/animate.css-3.7.2/animate.min.css">
head>
<body>
<div id="app">
<button @click="show = !show">
Toggle render
button>
<transition enter-active-class="animated bounceIn" leave-active-class="animated bounceOut" >
<p v-if="show">hellop>
transition>
div>
<script>// 2. 创建一个Vue的实例var vm = new Vue({el: '#app',data: {show: true
},
})script>
body>
html>
浏览器显示效果如下:
当点击隐藏「hello」的时候,使用「bounceOut」显示离场的效果。
![181a37e84263a4a80737c69b4275d0ff.png](https://i-blog.csdnimg.cn/blog_migrate/52afa491a6e031926ead5afe5fc20648.png)
当点击显示「hello」的时候,使用「bounceIn」显示入场的效果。
![8b0db424cd0ef59724e5a8e95f8be373.png](https://i-blog.csdnimg.cn/blog_migrate/33713c24ae668c3d63f90c2020762b45.png)
在上面可以看到两个class中都需要去写animated
,如下:
![91fe37ca25a0f69764dfa5746aa28187.png](https://i-blog.csdnimg.cn/blog_migrate/02873b23684b62be0e75917944a308ba.png)
能否优化一下呢?不用每个class都去写一遍,这样多麻烦。
优化animeted的填写位置
![d0ca3c6ca0980a00be31f179a2c3677f.png](https://i-blog.csdnimg.cn/blog_migrate/44961e16161150647cbfc2554ca6534f.png)
<transition enter-active-class="bounceIn" leave-active-class="bounceOut" >
<p v-if="show" class="animated">hellop>
transition>
显性的过渡持续时间
❝2.2.0 新增
❞
在很多情况下,Vue 可以自动得出过渡效果的完成时机。默认情况下,Vue 会等待其在过渡效果的根元素的第一个 transitionend
或 animationend
事件。然而也可以不这样设定——比如,我们可以拥有一个精心编排的一系列过渡效果,其中一些嵌套的内部元素相比于过渡效果的根元素有延迟的或更长的过渡效果。
在这种情况下你可以用 组件上的 duration
属性定制一个显性的过渡持续时间 (以毫秒计):
<transition :duration="1000">...transition>
你也可以定制进入和移出的持续时间:
<transition :duration="{ enter: 500, leave: 800 }">...transition>
使用:duration
设置动画统一的运行时长
上面只是设置了一些动画效果,但是如果需要设置动画的运行时长,那么则需要设置「duration」,如下:
![1575c2ed9accaeb52b3552c4f5ac7c55.png](https://i-blog.csdnimg.cn/blog_migrate/4b12b656e14b7661d8495278747abf56.png)
span style="line-height: 26px;">html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="lib/vue.js">script>
<link rel="stylesheet" href="lib/animate.css-3.7.2/animate.min.css">
head>
<body>
<div id="app">
<button @click="show = !show">
Toggle render
button>
<transition enter-active-class="bounceIn" leave-active-class="bounceOut" :duration="200">
<p v-if="show" class="animated">hellop>
transition>
<hr>
<transition enter-active-class="bounceIn" leave-active-class="bounceOut" :duration="1500">
<p v-if="show" class="animated">hellop>
transition>
div>
<script>// 2\. 创建一个Vue的实例var vm = new Vue({el: '#app',data: {show: true
},
})script>
body>
html>
浏览器执行如下:
![2adb551c60df185f5a11c6ac054cfd2e.png](https://i-blog.csdnimg.cn/blog_migrate/26a1a86abc2bed819f26b0ae2aac6e13.png)
上面设置入场和离场的运行时长都是一致的,如果需要拆分,可以单独设置如下。
使用:duration
分开设置动画的入场和离场的运行时长
使用字典就可以分开设置入场enter
和离场leave
的运行时长。
![16db37029cfa916de379db157c6ed829.png](https://i-blog.csdnimg.cn/blog_migrate/e2919a27a5ee6255bfbb1e5167a7f4c4.png)
<transition enter-active-class="bounceIn" leave-active-class="bounceOut":duration="{ enter:200, leave:1500 }">
<p v-if="show" class="animated">hellop>
transition>
交流QQ群:
![f647267c9686c1184fe673d78cd93e0d.png](https://i-blog.csdnimg.cn/blog_migrate/fae7c740547edc7111c23620c88dd827.png)
点击下面,查看更多Vue系列文章