animation 属性
animation 用来设置动画的名称、执行时间等。
animation-name:动画名称。
animation-duration:动画执行时间。
animation-iteration-count:动画执行次数,infinite -- 一直执行。
animation-fill-mode:动画执行之前或之后的效果是否可见,forwards -- 动画执行完之后不会变成原来的效果。
animation-delay:动画延迟执行的时间。
animation-direction:动画下一次执行的顺序,alternate -- 奇数次正向播放,偶数次反向播放;reverse -- 反向播放;alternate-reverse -- 奇数次反向播放,偶数次正向播放。
animation-timing-function:动画执行的速度曲线,ease-in -- 缓慢开始,ease-out -- 缓慢结束,ease-in-out -- 缓慢开始缓慢结束。
animation 是一个复合属性,依次是:名称、时间、执行次数、动画之前或之后的效果是否可见、延迟时间、播放顺序、速度曲线。
@keyframes 属性用来设置具体的动画
语法:@keyframes 动画名 { from(开始){ css效果 } to(结束){ css效果 } }
也可以:@keyframes 动画名 { 25%{ css效果 } 50%{ css效果 } 75{ css效果 } }
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>动画</title>
<style>
body {
background: #555;
}
.box {
background: cyan;
width: 200px;
height: 200px;
/* 动画名称 */
animation-name: anima1;
/* 动画执行时间 */
animation-duration: 3s;
/* 动画执行次数 infinite是一直执行 */
animation-iteration-count: infinite;
/* 动画执行之前或之后的效果是否可见 forwards:动画执行完之后不会变成原来的效果 */
animation-fill-mode: forwards;
/* 动画的延迟执行时间 */
/* animation-delay: 1s; */
/* 动画下一次执行的顺序。alternate指的是奇数次正向播放,偶数次反向播放 */
/* reverse:反向播放 */
/* alternate-reverse:奇数次反向播放,偶数次正向播放 */
animation-direction: alternate-reverse;
/* 速度曲线 */
/* ease-in:缓慢开始 ease-out:缓慢结束 ease-in-out:缓慢开始缓慢结束 */
animation-timing-function: ease-in-out;
position: relative;
/* animation: 名称 时间 执行次数 动画之前或之后的效果是否可见 延迟时间 播放顺序 速度曲线; */
}
/* 动画 */
@keyframes anima1 {
/* 开始 */
from {
width: 200px;
background: red;
top: 0;
left: 0;
}
/* 结束 */
to {
width: 600px;
background: cyan;
top: 200px;
left: 100px;
}
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
<style>
body {
background: #555;
}
.box {
background: #fff;
width: 200px;
height: 200px;
position: relative;
top: 0;
left: 0;
animation: anima1 5s infinite forwards ease-in-out;
}
/* 动画 */
@keyframes anima1 {
25% {
top: 0;
left: 300px;
background: red;
border-radius: 50% 0 0 0;
}
50% {
top: 300px;
left: 300px;
background: cyan;
border-radius: 0 50% 0 0;
}
75% {
top: 300px;
left: 0%;
background: gray;
border-radius: 0 0 50% 0;
}
100% {
top: 0;
left: 0;
border-radius: 0 0 0 50%;
}
}
</style>