动画就是定义dom元素按照指定好的指令去产生动作。对于我来说,感觉就是高级版的【过渡】,也可以和过渡结合使用。
先看一个效果
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div {
width: 100px;
height: 100px;
background: #007AFF;
/*动画名称*/
animation-name: moveFrame,colorFrame;
/*动画时长*/
animation-duration: 5s;
}
@keyframes moveFrame {
0% {
transform: translate(0, 0);
}
25% {
transform: translate(200px, 0);
}
50% {
transform: translate(200px, 200px);
}
75% {
transform: translate(0px, 200px);
}
100% {
transform: translate(0px, 0px);
}
}
@keyframes colorFrame {
from{
background: red;
}
to{
background: yellow;
}
}
</style>
</head>
<body>
<div></div>
</body>
</html>
当然动画不止这些功能还有一些其他属性可以配置
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div {
width: 100px;
height: 100px;
background: #007AFF;
/*动画名称*/
animation-name: moveFrame,colorFrame;
/*时间函数*/
animation-timing-function: linear;
/*动画时长*/
animation-duration: 5s;
/*动画次数 infinite为无限次*/
animation-iteration-count: infinite;
/*animation-iteration-count: 1;*/
/*交替动画 将动画逆倒着走一遍*/
animation-direction: alternate;
/*延迟*/
/*animation-delay: 0.1s;*/
/*animation-fill-mode: forwards; 保持动画结束时的状,不会还原*/
/*animation-fill-mode: backwards;如果动画有初始状态会立马进入状态*/
}
@keyframes moveFrame {
0% {
transform: translate(0, 0);
}
25% {
transform: translate(200px, 0);
}
50% {
transform: translate(200px, 200px);
}
75% {
transform: translate(0px, 200px);
}
100% {
transform: translate(0px, 0px);
}
}
@keyframes colorFrame {
from{
background: red;
}
to{
background: yellow;
}
}
</style>
</head>
<body>
<div></div>
</body>
</html>