为保证运行正常,咱先规定下:
* {
box-sizing: border-box;
}
1. 平滑加载
<div class="progress-1"></div>
.progress-1 {
width:120px;
height:20px;
background:
linear-gradient(#000 0 0) 0/0% no-repeat
#ddd;
animation:p1 2s infinite linear;
}
@keyframes p1 {
100% {background-size:100%}
}
linear-gradient(#000 0 0)
你可以理解为linear-gradient(#000 0 100%)
,如果还不熟悉,复制linear-gradient(#000 0 50%, #f00 50% 0)
,替换原先的部分跑一下。觉得linear-gradient(#000 0 0)
别扭的话,直接写#000
即可。0/0%
是background-position: 0;/background-size: 0;
的简写。
2.按步加载
<div class="progress-2"></div> .progress-2 { width:120px; height:20px; border-radius: 20px; background: linear-gradient(orange 0 0) 0/0% no-repeat lightblue; animation:p2 2s infinite steps(10); } @keyframes p2 { 100% {background-size:110%} }
steps(10)
是step(10, end)
的简写,指明刚开始没有,所以有第2点的处理100% {background-size:110%}
添加多一个step
的百分比,上面的step
是10
,所以是100% + (1/10)*100% = 110%
3.条纹加载
<div class="progress-3"></div>
.progress-3 {
width:120px;
height:20px;
border-radius: 20px;
background:
repeating-linear-gradient(135deg,#f03355 0 10px,#ffa516 0 20px) 0/0% no-repeat,
repeating-linear-gradient(135deg,#ddd 0 10px,#eee 0 20px) 0/100%;
animation:p3 2s infinite;
}
@keyframes p3 {
100% {background-size:100%}
}
repeating-linear-gradient(135deg,#ddd 0 10px,#eee 0 20px) 0/100%;
画出灰色的斑马线条纹,repeating-linear-gradient(135deg,#f03355 0 10px,#ffa516 0 20px) 0/0% no-repeat
则是进度条加载的条纹。
4.虚线加载
<div class="progress-4"></div>
.progress-4 {
width:120px;
height:20px;
-webkit-mask:linear-gradient(90deg,#000 70%,#0000 0) 0/20%;
background:
linear-gradient(#000 0 0) 0/0% no-repeat
#ddd;
animation:p4 2s infinite steps(6);
}
@keyframes p4 {
100% {background-size:120%}
}
-webkit-mask
默认有值 repeat
,不然遮罩不会有五个。
5.电池加载
<div class="progress-5"></div>
.progress-5 {
width:80px;
height:40px;
border:2px solid #000;
padding:3px;
background:
repeating-linear-gradient(90deg,#000 0 10px,#0000 0 16px)
0/0% no-repeat content-box content-box;
position: relative;
animation:p5 2s infinite steps(6);
}
.progress-5::before {
content:"";
position: absolute;
top: 50%;
left:100%;
transform: translateY(-50%);
width:10px;
height: 10px;
border: 2px solid #000;
}
@keyframes p5 {
100% {background-size:120%}
}
.progress-5::before {
content:"";
position: absolute;
top:-2px;
bottom:-2px;
left:100%;
width:10px;
background:
linear-gradient(
#0000 calc(50% - 7px),#000 0 calc(50% - 5px),
#0000 0 calc(50% + 5px),#000 0 calc(50% + 7px),#0000 0) left /100% 100%,
linear-gradient(#000 calc(50% - 5px),#0000 0 calc(50% + 5px),#000 0) left /2px 100%,
linear-gradient(#0000 calc(50% - 5px),#000 0 calc(50% + 5px),#0000 0) right/2px 100%;
background-repeat:no-repeat;
}
#0000 是透明,同等 transparent