CSS3 Animation Steps

CSS3 animation steps

steps() 和 cubic-bezier() 一样,都是 animation-timing-function 的属性值,用于规定动画的速度曲线。

语法

1
steps(number_of_steps, direction)
  • number_of_steps

    表示一个周期的动画分为多少阶段

  • direction

    通过关键字指定动画的连续方式

    • start

      动画为 left-continuous,在动画开始时执行第一阶段的动效,也就是不会显示第一阶段的动画效果

    • end

      默认值,动画为 right-continuous,在动画开始时执行最后一阶段的动效,即不显示最后一阶段的动画效果

step-startsteps(1, start) 的简写

step-endsteps(1)steps(1, end) 的简写

​ start 和 end 的区别可以看这个的 Demo

实现效果

按钮 loading

1
<button class="btn">提交中</button>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
body {
text-align: center;

.btn {
border: 1px solid #ccc;
background-color: #fff;
padding: 5px 10px;
min-width: 100px;
margin: 50px;
cursor: pointer;

&::after {
content: "";
width: 2px;
height: 2px;
display: inline-block;
margin-left: 3px;
animation: dotting 3s step-start infinite;
}
}
}

@keyframes dotting {
25% {
box-shadow: 2px 0;
}
50% {
box-shadow: 2px 0, 6px 0;
}
75% {
box-shadow: 2px 0, 6px 0, 10px 0;
}
}

打字效果

1
<p class="content">青青子衿,悠悠我心;但为君故,沉吟至今。</p>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
.content {
font-family: Consolas, monospace;
font-size: 150%;

position: relative;
display: inline-block;

&::after {
content: "|";
position:absolute;
top:0;
right:0;
width:0;
background: white;
animation:
caret 2s step-end infinite,
type 15s steps(20, start) infinite;
}
}

@keyframes type {
from { width: 100% }
to { width: 0 }
}
@keyframes caret {
0%,
100% {
color: transparent;
}

50% {
color: black;
}
}

雪碧图动画效果

1
2
3
4
5
6
<div class="heart"></div>

<div class="container">
<div class="front"></div>
<div class="rear"></div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
body {
display: flex;
flex-direction: column;
align-items: center;

.heart {
width: 100px;
height: 100px;
margin: 20px;
background: url(https://blog-1255677601.cossh.myqcloud.com/blog/2019-06-13-021016.jpg) no-repeat;
animation: front 5s steps(28) infinite;
}
}

.container {
display: inline-block;
position: relative;
text-align: center;

.front {
width: 100px;
height: 100px;
border: 1px solid black;
background: url(https://blog-1255677601.cossh.myqcloud.com/blog/2019-06-13-021016.jpg) no-repeat;
animation: front 5s steps(28) infinite;
}

.rear {
opacity: .2;
position: absolute;
z-index: -1;
top: 0;
left: 0;
width: 2900px;
height: 100px;
border: 1px solid #ccc;
background: url(https://blog-1255677601.cossh.myqcloud.com/blog/2019-06-13-021016.jpg) no-repeat left top;
animation: rear 5s steps(28) infinite;
}
}

@keyframes front {
0% { background-position: 0px 0; }
100% { background-position: -2800px 0; }
}

@keyframes rear {
0% { transform: translateX(0); }
100% { transform: translateX(-2800px); }
}

参考