赞
踩
创建动画序列,需要使用 animation
属性或其子属性,该属性允许配置动画时间、时长以及其他动画细节,但该属性不能配置动画的实际表现,动画的实际表现是由 @keyframes
规则实现
属性 | 描述 |
---|---|
animation-name | 指定由 @keyframes 描述的关键帧名称 |
animation-duration | 设置动画一个周期的时长 |
animation-delay | 设置延时,即从元素加载完成之后到动画序列开始执行的这段时间 |
animation-direction | 设置动画在每次运行完后是反向运行还是重新回到开始位置重复运行 |
animation-iteration-count | 设置动画重复次数, 可以指定 infinite 无限次重复动画 |
animation-play-state | 允许暂停和恢复动画 |
animation-timing-function | 设置动画速度, 即通过建立加速度曲线,设置动画在关键帧之间是如何变化 |
animation-fill-mode | 规定元素在不播放动画时的样式(在开始前、结束后,或两者同时) |
@keyframes test {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
@keyframes test {
from {
opacity: 1;
}
to {
opacity: 0;
}
}
当然,当我们的关键帧不止 2 帧的时,更推荐使用百分比定义的方式
除此之外,当动画的起始帧等同于 CSS 规则中赋予的值并且没有设定 animation-fill-mode
,0% 和 from 这一帧是可以删除的
animation-delay
就比较有意思了,它可以设置动画延时,即从元素加载完成之后到动画序列开始执行的这段时间
animation
属性,也可以简写为 animation: test 2s 1s
,第一个时间值表示持续时间,第二个时间值表示延迟时间div {
width: 100px;
height: 100px;
background: #000;
animation-name: test;
animation-duration: 2s;
}
div:nth-child(2) {
animation-delay: 1s;
}
@keyframes test {
0% {
transform: translate(0);
}
100% {
transform: translate(200px);
}
}
关于 animation-delay
,最有意思的技巧在于,它可以是负数。也就是说,虽然属性名是动画延迟时间,但是运用了负数之后,动画可以提前进行
.div:nth-child(1) {
animation: test 3s infinite linear;
}
.div:nth-child(2) {
animation: test 3s infinite -1s linear;
}
.div:nth-child(3) {
animation: test 3s infinite -2s linear;
}
同一个动画,我们利用一定范围内随机的 animation-duration
和一定范围内随机的 animation-delay
,可以有效的构建更为随机的动画效果,让动画更加的自然
animation-direction 属性可接受以下值:
我们利用 sass 的循环和 random() 函数,让 animation-duration
在 2-4 秒范围内随机,让 animation-delay
在 1-2 秒范围内随机,这样,我们就可以得到非常自然且不同的上升动画效果,基本不会出现重复的画面,很好的模拟了随机效果
@for $i from 1 to 11 {
li:nth-child(#{$i}) {
animation-duration: #{random(2000) / 1000 + 2}s;
animation-delay: #{random(1000) / 1000 + 1}s;
}
}
缓动函数在动画中非常重要,它定义了动画在每一动画周期中执行的节奏
缓动主要分为两类:
animation-timing-function 属性可接受以下值:
这里有个非常好用的网站 – https://cubic-bezier.com/ 用于创建和调试生成不同的贝塞尔曲线参数
步骤缓动函数的几种表现形态
{
animation-timing-function: step-start;
animation-timing-function: step-end;
animation-timing-function: steps(6, start)
animation-timing-function: steps(4, end);
}
animation-timing-function: steps(6)
可以将其用一个 CSS 动画串联起来.box {
width: 256px;
height: 256px;
background: url('@/assets/img/test.jpg');
animation: test 0.6s steps(6, end) infinite;
}
@keyframes test {
0% {
background-position: 0 0;
}
100% {
background-position: -1536px 0;
}
}
animation
: sprite .6s steps(6) infinite 动画@keyframes
动画分为 6 次(6 帧)执行,而整体的动画时间是 0.6s,所以每一帧的停顿时长为 0.1sbackground-position
: 0 0 到 background-position
: -1536px 0,由于上述的 CSS 代码没有设置 background-repeat
,所以其实 background-position
: 0 0 是等价于 background-position
: -1536px 0,就是图片在整个动画过程中推进了一轮,只不过每一帧停在了特点的地方,一共 6 帧background-position
的取值其实只有 background-position
: 0 0,background-position
: -256px 0,background-position
: -512px 0 依次类推一直到 background-position
: -1536px 0,由于背景的 repeat
的特性,其实刚好回到原点,由此又重新开始新一轮同样的动画同个动画效果的补间动画和逐帧动画演绎对比
steps
将补间动画变成逐帧动画.box {
animation: test 2s steps(10) infinite;
}
@keyframes test {
0% {
transform: rotate(0);
}
100% {
transform: rotate(360deg);
}
}
animation-play-state
,顾名思义,它可以控制动画的状态 – 运行或者暂停,类似于视频播放器的开始和暂停,是 CSS 动画中有限的控制动画状态的手段之一
它的取值只有两个(默认为 running):animation-play-state
: paused | running;
使用起来也非常简单,看下面这个例子,我们在 hover 按钮的时候,实现动画的暂停:
.box {
width: 100px;
height: 100px;
background: deeppink;
animation: test 2s linear infinite alternate;
}
@keyframes test {
100% {
transform: translate(100px, 0);
}
}
.stop:hover ~ .box {
animation-play-state: paused;
}
animation-fill-mode
属性可接受以下值:
none
- 默认值。动画在执行之前或之后不会对元素应用任何样式forwards
- 元素在动画开始之前的样式为 CSS 规则设定的样式,而动画结束后的样式则表现为由执行期间遇到的最后一个关键帧计算值(也就是停在最后一帧)backwards
- 元素在动画开始之前(包含未触发动画阶段及 animation-delay 期间)的样式为动画运行时的第一帧,而动画结束后的样式则恢复为 CSS 规则设定的样式both
- 综合了 animation-fill-mode: backwards 和 animation-fill-mode: forwards 的设定。动画开始前的样式为动画运行时的第一帧,动画结束后停在最后一帧animation-iteration-count
控制动画运行的次数,可以是数字或者 infinite
,注意,数字可以是小数
animation-direction
控制动画的方向,正向、反向、正向交替与反向交替
在上面讲述 animation-fill-mode
时,我使用了动画运行时的第一帧替代了@keyframes
中定义的第一帧这种说法,因为动画运行的第一帧和最后一帧的实际状态还会受到动画运行方向 animation-direction
和 animation-iteration-count
的影响
在 CSS 动画中,由 animation-iteration-count
和 animation-direction
共同决定动画运行时的第一帧和最后一帧的状态
animation-direction
决定animation-iteration-count
和 animation-direction
决定.box {
animation: test 4s linear;
animation-play-state: paused;
transform: translate(0, 0);
}
@keyframes test {
0% {
transform: translate(100px, 0);
}
100% {
transform: translate(300px, 0);
}
}
元素没有设置位移 transform: translate(0, 0),而在动画中,第一个关键帧和最后一个关键的 translateX 分别是 100px、300px,配合不同 animation-direction 初始状态如下
这里我们实现了一个 div 块下落动画,下落的同时产生透明度的变化:
div {
width: 100px;
height: 100px;
background: #000;
animation: combine 2s;
}
@keyframes combine {
100% {
transform: translate(0, 150px);
opacity: 0;
}
}
/* or */
div {
animation: falldown 2s, fadeIn 2s;
}
@keyframes falldown {
100% {
transform: translate(0, 150px);
}
}
@keyframes fadeIn {
100% {
opacity: 0;
}
}
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。