css动画

1.利用css完成一个秒表动画,需要注意的地方:::before以及::after伪元素必须设置content:’’。

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
<div class="stopwatch">
<div class="stopwatch-top"></div>
</div>
<style type="text/css">
.stopwatch {
width: 30px;
height: 30px;
background-color: transparent;
border-radius: 50% 50%;
border: 2px solid #fff;
position: relative;
}
.stopwatch::before, .stopwatch::after {
content: '';
position: absolute;
top: 14px;
left: 15px;
height: 2px;
background-color: #fff;
transform-origin: 1px 1px; // 分针以及时针的旋转中心点
}
/* 分针 */
.stopwatch::before {
width: 15px;
animation: fen 1s linear infinite; // 1s内完成动画,匀速,无限动画
}
/* 时针 */
.stopwatch::after {
width: 9px;
animation: hour 60s linear infinite; // 同理
}
@keyframes fen {
0% {transform: rotate(0deg);}
100% {transform: rotate(360deg);}
}
@keyframes hour {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
/* 秒表顶部柱头,也可以不画 */
.stopwatch-top {
position: absolute;
width: 2px;
height: 6px;
left: 14px;
top: -6px;
background-color: #fff;
}
</style>

2.电池充电动画

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
<div class="battery"></div>
<style type="text/css">
.battery {
width: 40px;
height: 20px;
border: 2px solid #fff;
border-radius: 2px;
position: relative;
background-color: transparent;
animation: battery 5s linear infinite;
}
/* 电池正极 */
.battery::after {
content: '';
width: 2px;
height: 10px;
background-color: #fff;
border: 0 2px 2px 0;
position: absolute;
top: 5px;
right: -4px;
}
/* 充电动画,利用box-shadow: inset */
@keyframes battery {
0% { box-shadow: inset 0 0 0 #fff; };
100% { box-shadow: inset 40px 0 0 #fff; };
}
</style>