animation 动画
1. 官方定义
animation-name
animation-duration
animation-timing-function
animation-delay
animation-iteration-count
animation-direction
animation-fill-mode
注意:请始终规定 animation-duration
属性,否则时长为 0,就不会播放动画了。
2. 解释
animation
是几个属性值的缩写,我们定义一个动画通常使用 animation
就足够了,如果想单独改变动画的某些属性可以使用单独的动画属性去改变,构成一个动画的最基本属性需要 一个@keyframes
和 duration
。
3. 语法
.demo{
animation: name duration timing-function delay iteration-count direction;
}
属性值说明:
animation-direction 参数值详解:
animation-fill-mode
参数值详解
4. 兼容性
IE | Edge | Firefox | Chrome | Safari | Opera | ios | android |
---|---|---|---|---|---|---|---|
9+ | 12+ | 28+ | 4+ | 6.1+ | 12.1+ | 7+ | 4.4 |
5. 实例
- 使用
from to
定义一个名字为go
的@keyframes
的动画。
@keyframes go{
from{
width:px;
}
to{
width:px
}
}
.demo{
width:px;
height:px;
background: #000;
animation:go s ;
}
效果图
@keyframes go{
from{
width:px;
}
to{
width:px
}
}
.demo{
width:px;
height:px;
background: #000;
animation:go s ease-in;/*这里增加了动画函数*/
}
效果图
Tips:要注意的是,不管怎么改变动画的结束时间是不会变的。具体可以看 timing-function 的介绍。
@keyframes go{
from{
width:px;
}
to{
width:px
}
}
.demo{
width:px;
height:px;
background: #000;
animation:go s ease-in s;/*这里增加了动画延迟时间 3 秒*/
}
效果图
说明:动画延迟了 3 秒开始再次执行了。
动画延迟 3s 开始播放,播放 2 次结束。
@keyframes go{
from{
width:px;
}
to{
width:px
}
}
.demo{
width:px;
height:px;
background: #000;
animation:go s ease-in s ;/*播放 2 次结束*/
}
效果图
动画延迟 3s 开始播放,然后无限循环。
@keyframes go{
from{
width:px;
}
to{
width:px
}
}
.demo{
width:px;
height:px;
background: #000;
animation:go s ease-in s infinite;/*无限次循环*/
}
效果图
说明:通过 infinite
动画在经过 3s
的延迟之后开始无限的循环了。
-
animation-direction
来改变动画在循环过程中是否反向。
延续上面的例子,我们发现动画每次循环都是从开始的位置开始循环的,下面通过添加 animation-direction
来改变动画在循环过程中是否反向。
@keyframes go{
from{
width:px;
}
to{
width:px
}
}
.demo{
width:px;
height:px;
background: #000;
animation:go s ease-in s infinite reverse;/*动画反向播放*/
}
使用 alternate
属性,让动画在奇数时候正向播放,偶数时候反向播放。
@keyframes go{
from{
width:px;
}
to{
width:px
}
}
.demo{
width:px;
height:px;
background: #000;
animation:go s ease-in s infinite alternate;
}
效果图
<!DOCTYPE html>
<html lang="en">
<head>
<Meta charset="UTF-8">
<Meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
@keyframes go{
from{
width:px;
}
to{
width:px
}
}
.demo{
width:px;
height:px;
background: #000;
animation:go s ease-in s infinite;/*动画反向播放*/
}
.demo-1{
width:px;
height:px;
background: #000;
animation:go s ease-in s infinite reverse;/*动画反向播放*/
}
.demo-2{
width:px;
height:px;
background: #000;
animation:go s ease-in s infinite alternate;/*动画偶数反向播放*/
}
</style>
</head>
<body>
<h2>正常播放顺序</h2>
<div class="demo"></div>
<h2>反向播放顺序</h2>
<div class="demo-1"></div>
<h2>奇数正向播放偶数次反向播放</h2>
<div class="demo-2"></div>
</body>
</html>
-
animation-fill-mode
设置动画的初始或结束状态。
单次动画使用 forwards
设置动画结束之后使用结束后的状态作为样式。
@keyframes go{
from{
width:px;
}
to{
width:px
}
}
.demo{
width:px;
height:px;
background: #000;
animation:go s ease-in s forwards;
}
在设置延迟之后元素中使用 backwards
设置动画的开始的样式。
@keyframes go{
from{
width:px;
}
to{
width:px
}
}
.demo{
width:px;
height:px;
background: #000;
animation:go s ease-in s backwards;
}
效果图
<!DOCTYPE html>
<html lang="en">
<head>
<Meta charset="UTF-8">
<Meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
@keyframes go{
from{
width:px;
}
to{
width:px
}
}
.demo{
width:px;
height:px;
background: #000;
animation:go s ease-in s ;/*动画反向播放*/
}
.demo-1{
width:px;
height:px;
background: #000;
animation:go s ease-in s forwards;
}
.demo-2{
width:px;
height:px;
background: #000;
animation:go s ease-in s backwards;
}
</style>
</head>
<body>
<h2>正常动画</h2>
<div class="demo"></div>
<h2>设置 forwards</h2>
<div class="demo-1"></div>
<h2>设置 backwards 注意观察开始</h2>
<div class="demo-2"></div>
</body>
</html>
说明:在鼠标刷新浏览器我们看到本应该 100px 宽度的元素是以 200px 开始的,当动画结束之后,回到了 100px。
both
在设置动画延迟情况下,元素使用开始的状态,并在整个动画结束之后使用结束之后的状态。
@keyframes go{
from{
width:px;
}
to{
width:px
}
}
.demo{
width:px;
height:px;
background: #000;
animation:go s ease-in s both;
}
效果图
<!DOCTYPE html>
<html lang="en">
<head>
<Meta charset="UTF-8">
<Meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
@keyframes go{
from{
width:px;
}
to{
width:px
}
}
.demo{
width:px;
height:px;
background: #000;
animation:go s ease-in s ;/*动画反向播放*/
}
.demo-3{
width:px;
height:px;
background: #000;
animation:go s ease-in s both;
}
</style>
</head>
<body>
<h2>正常动画</h2>
<div class="demo"></div>
<h2>设置 both 注意观察开始和结束</h2>
<div class="demo-3"></div>
</body>
</html>
6. 经验分享
<div class="demo">
往事不要再提<span>人生已多风雨</span>
</div>
.demo{
cursor: pointer;
}
.demo>span{
display: none;
}
.demo:hover>span{
display: block;
animation: shows s forwards;
}
@keyframes shows {
from{
opacity: ;
}to{
opacity: ;
}
}
效果图
说明: transition
不能实现(隐藏/显示)之间的过渡效,原因是 diaplay:none
设置之后虽然元素在页面中,但是这个标签不在浏览器的渲染进程里面。如果这个元素属性为 display:block
相当于元素从新渲染出来,这时里面的 opacity: 0
到 1 就不起作用了。所以这里使用 animation
正好可以弥补这个问题。