一、通过border-radius绘制圆环
<div class="loading-css"></div>.loading-css { width: 50px; /*先将loading区域变成正方形*/ height: 50px; display: inline-block; /*将loading区域变成行内元素,避免旋转的时候,100%宽度都在旋转*/ border: 3px solid #f3f3f3; /*设置周围边框大小,并将色彩设置为浅白色*/ border-top: 3px solid red; /*将上边框色彩设置为红色高亮,以便旋转的时候可能看到旋转的成果*/ border-radius: 50%; /*将边框和内容区域都变成圆形*/}@keyframes loading-360 { 0% { transform: rotate(0deg); /*动画起始的时候旋转了0度*/ } 100% { transform: rotate(360deg); /*动画完结的时候旋转了360度*/ }}.loading-css { /*在之前的CSS中加上动画成果即可*/ animation: loading-360 0.8s infinite linear; /*给圆环增加旋转360度的动画,并且是有限次*/}
此时成果如下:
二、通过svg来绘制圆环
<svg viewBox="0 0 50 50" class="loading-svg"> <circle cx="25" cy="25" r="20" fill="none" class="path"></circle></svg>.loading-svg { width: 50px; /*设置svg显示区域大小*/ height: 50px; animation: loading-rotate 1.5s infinite ease-in-out; /*给svg也加上一个旋转动画*/}.path { stroke: #409eff; /*给画笔设置一个色彩*/ stroke-width: 2; /*设置线条的宽度*/ stroke-dasharray: 95, 126; /*设置实现长95,虚线长126*/ stroke-dashoffset: 0; /*设置虚线的偏移地位*/ animation: loading-dash 1.5s ease-in-out infinite;}@keyframes loading-dash { 0% { stroke-dasharray: 1, 126; /*实线局部1,虚线局部126*/ stroke-dashoffset: 0; /*后面1/126显示实线,前面125显示空白*/ } 50% { stroke-dasharray: 95, 126; /*实线局部95,虚线局部126*/ stroke-dashoffset: -31px /*顺时针偏移31/126,即前31/126显示空白,前面3/4显示线条*/ } to { stroke-dasharray: 6, 120; /*实线局部6,虚线局部120*/ stroke-dashoffset: -120px; /*最初顺时针偏移120/126,即前120/126显示空白,前面6点显示线条局部*/ }}@keyframes loading-rotate { to { transform: rotate(1turn); // 旋转1圈 }}
此时成果如下:
三、通过iconfont字体图标
能够间接通过iconfont字体图标代替圆环的绘制,间接以字体的模式显示出圆环,而后给其加上旋转动画即可,如:
咱们能够在iconfont网站上下载喜爱的Loading图案。字体图标下载后,将解压后的内容拷贝到我的项目中,并引入其中的iconfont.css到页面中,给要显示字体图标的元素加上iconfont类款式
具体代码引入形式,可参考demo_index.html文件,外面有三种引入形式,我这边目前采纳的是第二种。
代码如下:
<link rel="stylesheet" href="./font/iconfont.css"><span class="icon-Loading iconfont"></span>.icon-Loading { display: inline-block; color: #0086B3; font-size: 40px; animation: rotating 2s infinite linear;}@keyframes rotating { 0% { transform: rotate(0deg) /*动画起始地位为旋转0度*/ } to { transform: rotate(1turn) /*动画完结地位为旋转1圈*/ }}
此时成果如下: