动画
动画(animation) 是css中具有颠覆性的特征之一,可以通过设置多个节点来精确控制一个, 或一组动画
常用来实现复杂的动画效果。
相比较过渡,动画可以实现更多变化,更多控制,连续自动播放等效果。
1. 动画的基本使用
制作动画分为两步:
- 先定义动画
- 再使用(调用)动画
用keyfames定义动画(类似定义类选择器)
@keyframes 动画名称{
0%{
width:100px;
}
100%{
width:200px;
}
}
动画基本使用命令:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
/* 我们想页面一打开,一个盒子就从左边走到右边 */
/* 1. 定义动画 */
@keyframes move{
/* 开始状态 */
0%{
transform: translateX(0px);
}
/* 结束状态 */
100%{
transform: translateX(1000px);
}
}
div{
width:200px;
height: 200px;
background-color: greenyellow;
/* 2.调用动画 */
/* 动画名称 */
animation-name: move;
/* 持续时间 */
animation-duration: 2s;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
网页显示图:
动画序列
1. 0%是动画的开始,100%是动画的完成。这样的规则就是动画序列。
2. 在@keyframes中规定某项CSS样式,就能创建由当前未央市住建改为新样式的动画效果。
3. 动画是使元素从一种样式逐渐变化为另一种样式的效果。您可以改变任意多的样式任意多的次数。
4. 请用百分比来规定变化发生的时间,或用关键词“from”和“to”,等同于0%和100%。
动画序列命令:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
/* from 和 to 等价于 0% 和 100% */
@keyframes move{
from{
transform: translate(0,0);
}
to{
transform: translate(1000px,0);
}
}
/* 动画序列 */
div{
width: 100px;
height: 100px;
background-color: greenyellow;
animation-name: move;
animation-duration: 2s;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
网页显示图:
动画序列案例命令:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
/* 动画序列 */
/* 1. 可以做多个状态的变化 keyframes 关键帧*/
/* 2. 里面的百分比要是整数 */
/* 3. 里面的百分比就是 总得时间(我们这个案例10s)的划分 */
@keyframes move{
0%{
transform: translate(0,0);
}
25%{
transform: translate(1000px,0);
}
50%{
transform: translate(1000px,500px);
}
75%{
transform: translate(0,500px);
}
100%{
transform: translate(0,0);
}
}
div{
width: 100px;
height: 100px;
background-color: greenyellow;
animation-name: move;
animation-duration: 10s;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
网页显示图:
2. 元素使用动画
div{
width:200px;
height:200px;
background-color:aqua;
margin:100px auto;
/*调用动画*/
animation-name:动画名称;
/*持续时间*/
animation-duration:持续时间;
}
2. 动画常用属性
属性 | 描述 |
@keyframes | 规定动画。 |
animation | 所有动画属性的简写属性,除了animation-play-state属性。 |
animation-name | 规定@keyframes动画的名称。(必须的) |
animation-duration | 规定动画完成一个周期所花费的秒或毫秒,默认是0。(必须的) |
animation-timing-function | 规定动画的速度曲线,默认是“ease” |
animation-delay | 规定动画何时开始,默认是0. |
animation-iteration-count | 规定动画被播放的次数,默认是1, 还有infinite |
animation-direction | 规定动画是否在下一周期逆向播放,默认是“normal”,alternate逆播放 |
animation-play-state | 规定多那个计划是否正在运行或暂停。 默认是“running”,还有“pause” |
animation-fill-mode | 规定动画结束后状态,保持forwards回到起始backwards |
动画属性命令:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
@keyframes move {
0%{
transform: translate(0,0);
}
100%{
transform: translate(1000px,0);
}
}
div:hover{
/* 鼠标经过div 让这个div 停止动画, 鼠标离开就继续动画 */
animation-play-state: paused;
}
div{
width: 100px;
height: 100px;
background-color: greenyellow;
/* 动画名称 */
animation-name: move;
/* 持续时间 */
animation-duration: 2s;
/* 运动曲线 */
animation-timing-function: ease;
/* 何时开始 */
animation-delay: 1s;
/* 重复次数 iteration 重复的 count 次数 infinite 无限 */
/* animation-iteration-count: infinite; */
/* 是否反方向播放 默认的是 normal 如果想要反方向 就写 alternate*/
/* animation-direction: alternate; */
/* 动画结束后的状态 默认的是 backwards 回到起始状态 我们可以让它停留在结束状态 forwards */
/* animation-fill-mode: forwards; */
/* animation: name duration timing-function delay iteration-count direction fill-mode; */
animation: move 2s linear 0s 1 alternate forwards;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
网页显示图:
3. 动画简写属性
animation : 动画名称 持续时间 运动开始 播放次数 是否反方向 动画起始或者结束的状态;
animation: myfirst 5s linear 2s infinite alternate;
简写属性里面不包含 animation-play-state
暂停动画 : animation-play-state: puased; 经过和鼠标经过等其他配合使用
想要动画走回来, 而不是直接跳回来: animation-direction : alternate
盒子动画结束后,停在结束位置 : animation-fill-mode : forwards
动画简写属性:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
@keyframes move {
0%{
transform: translate(0,0);
}
100%{
transform: translate(1000px,0);
}
}
div:hover{
/* 鼠标经过div 让这个div 停止动画, 鼠标离开就继续动画 */
animation-play-state: paused;
}
div{
width: 100px;
height: 100px;
background-color: greenyellow;
/* animation: name duration timing-function delay iteration-count direction fill-mode; */
/* 前面2个属性 name duration 一定要写 */
animation: move 2s linear 0s 1 alternate forwards;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
网页显示图:
做一个地图热点图:
命令演示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
body{
background-color: #333;
}
.map{
position: relative;
width: 747px;
height: 616px;
background: url(images/map.png) no-repeat;
margin: 0 auto;
}
.city{
position: absolute;
top: 227px;
right: 193px;
color: #fff;
}
.tb{
top: 500px;
right: 80px;
}
.gz{
top: 532px;
right: 171px;
}
.dotted{
width: 8px;
height: 8px;
background-color: #09f;
border-radius: 50%;
}
.city div[class^="pulse"] {
/* 保证我们小波纹在父盒子里面水平垂直居中 放大之后就会中心向四周发散 */
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
width: 8px;
height: 8px;
box-shadow: 0 0 12px #009bfd;
border-radius: 50%;
animation: pulse 1s linear infinite;
}
.city div.pulse2{
animation-delay: 0.4s;
}
.city div.pulse3{
animation-delay: 0.6s;
}
@keyframes pulse{
0%{}
50%{
/* transform: scale(5); 我们不要用scale 因为它会让阴影变大 */
width: 30px;
height: 30px;
opacity: 1;
}
75%{
width: 45px;
height: 45px;
opacity: 0.6;
}
100%{
width: 60px;
height: 60px;
opacity: 0;
}
}
</style>
</head>
<body>
<div class="map">
<div class="city">
<div class="dotted"></div>
<div class="pulse1"></div>
<div class="pulse2"></div>
<div class="pulse3"></div>
</div>
<div class="city tb">
<div class="dotted"></div>
<div class="pulse1"></div>
<div class="pulse2"></div>
<div class="pulse3"></div>
</div>
<div class="city gz">
<div class="dotted"></div>
<div class="pulse1"></div>
<div class="pulse2"></div>
<div class="pulse3"></div>
</div>
</div>
</body>
</html>
网页显示图:
4. 速度曲线细节
animation-timing-function : 规定动画的速度曲线,默认是“ease”
值 | 描述 |
linear | 动画从头到尾的速度是相同的。匀速 |
ease | 默认。 动画以低速开始,然后加快,在结束前变慢。 |
ease-in | 动画以低速开始。 |
ease-out | 动画以低速结束。 |
ease-in-out | 动画以低速开始和结束。 |
steps() | 指定了时间函数中的间隔数量(步长) |
速度曲线步长命令:
!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
div{
overflow: hidden;
font-size: 40px;
width: 0;
height: 30px;
background-color: greenyellow;
/* 让我们的文字强制一行内显示 */
white-space: nowrap;
/* steps 就是分几步来完成我们的动画 有了steps 就不要再写 ease 或者 linear 了 */
animation: w 4s steps(5) forwards;
}
@keyframes w{
0%{
width: 0;
height: 50px;
}
100%{
width: 200px;
height: 50px;
}
}
</style>
</head>
<body>
<div>速度曲线步</div>
</body>
</html>
网页显示图: