animation是一个简写的动画属性由三部分组成
1.关键帧(keyframes) - 定义动画在不同阶段的状态
2.动画属性(properties) - 决定动画的播放时长,播放次数,以及用何种函数式去播放动画等。
3.css属性 - 就是css元素不同关键帧下的状态。
用于设置动画属性:
属性 | 描述 |
---|---|
animation-name | 规定需要绑定到选择器的 keyframe 名称。 |
animation-timing-function | 设置动画将如何完成一个周期 |
animation-duration | 动画指定需要多少秒或毫秒完成 |
animation-iteration-count | 定义动画的播放次数。 |
animation-direction | 指定是否应该轮流反向播放动画。 |
animation-fill-mode | 规定当动画不播放时(当动画完成时,或当动画有一个延迟未开始播放时),要应用到元素的样式 |
animation-play-state | 指定动画是否正在运行或已暂停。 |
用法:[[ animation-name ] || [ animation-duration ] || [ animation-timing-function ] || [ animation-delay ] || [ animation-iteration-count ] || [ animation-direction ]]
一关键帧(@keyframes)
关键帧的编写顺序没有要求,最后只会根据百分比按由小到大的顺序触发。
@keyframes color{
0% {
background-color: red;
}
50% {
background-color: blue;
}
100% {
background-color: green;
}
}
二动画属性(properties)
1.为 @keyframes 动画指定一个名称:
animation-name:color;
div{
width: 100px;
height: 100px;
background-color: black;
/* color 动画名称 */
animation: color 2s;
}
@keyframes color{
0% {
background-color: red;
}
50% {
background-color: blue;
}
100% {
background-color: green;
}
}
2.animation-timing-function 设置动画如何完成一个周期
animation-timing-function使用的数学函数,称为三次贝塞尔曲线,速度曲线。使用此函数,您可以使用您自己的值,或使用预先定义的值之一:
linear 动画从头到尾的速度是相同的。
ease 默认。动画以低速开始,然后加快,在结束前变慢
ease-in 动画以低速开始。
ease-out 动画以低速结束。
ease-in-out 动画以低速开始和结束。
cubic-bezier(n,n,n,n) 在 cubic-bezier 函数中自己的值。可能的值是从 0 到 1 的数值。
3.animation-duration:动画指定需要多少秒或毫秒完成
animation: color 2s;这个动画在两秒以内完成
4.animation-iteration-count:定义动画的次数
animation: color 2s 3;执行三次
5.animation-direction:指定是否应该轮流反向播放动画。
normal 默认值。动画按正常播放。
reverse 动画反向播放。
alternate 动画在奇数次(1、3、5…)正向播放,在偶数次(2、4、6…)反向播放。
alternate-reverse 动画在奇数次(1、3、5…)反向播放,在偶数次(2、4、6…)正向播放。
initial 设置该属性为它的默认值。
inherit 从父元素继承该属性。
6.animation-fill-mode:规定当动画不播放时(当动画完成时,或当动画有一个延迟未开始播放时),要应用到元素的样式
none 默认值。动画在动画执行之前和之后不会应用任何样式到目标元素。
forwards 在动画结束后(由 animation-iteration-count 决定),动画将应用该属性值。
backwards 动画将应用在 animation-delay 定义期间启动动画的第一次迭代的关键帧中定义的属性值。这些都是 from 关键帧中的值(当 animation-direction 为 “normal” 或 “alternate” 时)或 to 关键帧中的值(当 animation-direction 为 “reverse” 或 “alternate-reverse” 时)。
both 动画遵循 forwards 和 backwards 的规则。也就是说,动画会在两个方向上扩展动画属性。
initial 设置该属性为它的默认值。
inherit 从父元素继承该属性。
7.animation-play-state: 指定动画是否正在运行或已暂停。
paused 指定暂停动画
running 指定正在运行的动画