纯 CSS 实现十个还不错的 Loading 效果

大厂技术  高级前端  Node进阶

点击上方 程序员成长指北,关注公众号

回复1,加入高级Node交流群

e68a7fd6fdc1c9e759764cd577f28ec8.png
loading_banner.png

在推特上面看到 T. Afif[1] 介绍的十个 Loading 效果。如上图。

Yeah,很赞哦,挺实用的,遂记录下来。

为保证运行正常,咱先规定下:

* {
  box-sizing: border-box;
}
复制代码

1. 平滑加载

03072553f7eb8a77edc04c1b25597bd9.png
1.平滑加载.gif
<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%}
}
复制代码
  1. linear-gradient(#000 0 0) 你可以理解为 linear-gradient(#000 0 100%),如果还不熟悉,复制 linear-gradient(#000 0 50%, #f00 50% 0) ,替换原先的部分跑一下。觉得 linear-gradient(#000 0 0) 别扭的话,直接写 #000 即可。

  2. 0/0%background-position: 0;/background-size: 0; 的简写。

2. 按步加载

54f720ec2394adbbf33a240f6de0ca27.png
2.按步加载.gif
<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%}
}
复制代码
  1. steps(10)step(10, end) 的简写,指明刚开始没有,所以有第2点的处理

  2. 100% {background-size:110%} 添加多一个 step 的百分比,上面的 step10,所以是100% + (1/10)*100% = 110%

3. 条纹加载

da31b7ea9627f07683a490e1f6cd72e1.png
3.条纹加载.gif
<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. 虚线加载

b8bd446e07dccb3aa96c9b3b1ffb2363.png
4.虚线加载.gif
<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. 电池加载

77391f65bf6202bbe48875d6e2fc465b.png
5.电池加载.gif
<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 伪元素实现如下:

.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

6. 内嵌加载

这名字起得有些不贴切,不过不重要,读者看图自然理解。

0759d8a759b0c231d3a2b1a739927513.png
6.内嵌加载.gif
<div class="progress-6"></div>
复制代码
.progress-6 {
  width:120px;
  height:22px;
  border-radius: 20px;
  color: #514b82;
  border:2px solid;
  position: relative;
}
.progress-6::before {
  content:"";
  position: absolute;
  margin:2px;
  inset:0 100% 0 0;
  border-radius: inherit;
  background: #514b82;
  animation:p6 2s infinite;
}
@keyframes p6 {
    100% {inset:0}
}
复制代码

inset:0 100% 0 0; 右边内缩 100%,所以在 keyframes 部分需要将 inset 设置为 0

7. 珠链加载

e66cb885dd7a039065827fbb831362cf.png
7.珠链加载.gif
<div class="progress-7"></div>
复制代码
.progress-7 {
  width:120px;
  height:24px;
  -webkit-mask:
    radial-gradient(circle closest-side,#000 94%,#0000) 0 0/25% 100%,
    linear-gradient(#000 0 0) center/calc(100% - 12px) calc(100% - 12px) no-repeat;
  background:
   linear-gradient(#25b09b 0 0) 0/0% no-repeat
   #ddd;
  animation:p7 2s infinite linear;
}
@keyframes p7 {
    100% {background-size:100%}
}
复制代码

遮罩 -webkit-maskradial-gradient 是将宽度四等份,每份以最小 closest-side 的边为直径画圆。

8. 斑马线加载

c9286a4111251def205ffd856ad026d5.png
8.斑马线加载圆.gif
<div class="progress-8"></div>
复制代码
.progress-8 {
  width:60px;
  height:60px;
  border-radius: 50%;
  -webkit-mask:linear-gradient(0deg,#000 55%,#0000 0) bottom/100% 18.18%;
  background:
   linear-gradient(#f03355 0 0) bottom/100% 0% no-repeat
   #ddd;
  animation:p8 2s infinite steps(7);
}
@keyframes p8 {
    100% {background-size:100% 115%}
}
复制代码

linear-gradient 描绘的角度做调整,再加上蒙版。

9. 水柱加载

1df476da3051b5453df1cfa4d9e89543.png
9.水柱加载.gif
<div class="progress-9"></div>
复制代码
.progress-9 {    
  --r1: 154%;
  --r2: 68.5%;
  width:60px;
  height:60px;
  border-radius: 50%; 
  background:
    radial-gradient(var(--r1) var(--r2) at top ,#0000 79.5%,#269af2 80%) center left,
    radial-gradient(var(--r1) var(--r2) at bottom,#269af2 79.5%,#0000 80%) center center,
    radial-gradient(var(--r1) var(--r2) at top ,#0000 79.5%,#269af2 80%) center right,
    #ccc;
  background-size: 50.5% 220%;
  background-position: -100% 0%,0% 0%,100% 0%;
  background-repeat:no-repeat;
  animation:p9 2s infinite linear;
}
@keyframes p9 {
    33%  {background-position:    0% 33% ,100% 33% ,200% 33% }
    66%  {background-position: -100%  66%,0%   66% ,100% 66% }
    100% {background-position:    0% 100%,100% 100%,200% 100%}
}
复制代码

radial-gradient 画出水平面的波动,就三个圆。var(--r1) 直接调用定义好的属性值。技能 get ...

10. 信号加载

2d5e7e8e1b3248f67408ddb34e7852c2.png
10.信号加载.gif
<div class="progress-10"></div>
复制代码
.progress-10 {
  width:120px;
  height:60px;
  border-radius:200px 200px 0 0;
  -webkit-mask:repeating-radial-gradient(farthest-side at bottom ,#0000 0,#000 1px 12%,#0000 calc(12% + 1px) 20%);
  background:
   radial-gradient(farthest-side at bottom,#514b82 0 95%,#0000 0) bottom/0% 0% no-repeat
   #ddd;
  animation:p10 2s infinite steps(6);
}
@keyframes p10 {
    100% {background-size:120% 120%}
}
复制代码

repeating-radial-gradient 方法画出环状的蒙版遮罩。radial-gradient 从底部向上圆形渐变填充。

Uha,看了这么多骚操作,你学废了吗?

参考和后话

  • 原文:10 CSS-only loaders ready to use\![2]

  • 文章首发 - blogs[3]

关于本文

作者:Jimmy

https://juejin.cn/post/7080542771387301896

Node 社群


我组建了一个氛围特别好的 Node.js 社群,里面有很多 Node.js小伙伴,如果你对Node.js学习感兴趣的话(后续有计划也可以),我们可以一起进行Node.js相关的交流、学习、共建。下方加 考拉 好友回复「Node」即可。

如果你觉得这篇内容对你有帮助,我想请你帮我2个小忙:

1. 点个「在看」,让更多人也能看到这篇文章2. 订阅官方博客 www.inode.club 让我们一起成长

点赞和在看就是最大的支持❤️
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值