[前端随笔][CSS] 制作一个加载动画 即帖即用

说在前面

描述 [加载中loading...] 的动画图片往往使用GIF来实现,但GIF消耗资源较大,所以使用CSS直接制作更优。

效果传送门1  效果传送门2


关键代码

@keyframes 规则  用于设定动画规则  <文档传送门>

animation 属性    用于设定动画    <文档传送门>

transform 属性    用于设定旋转角度  <文档传送门>


正文

效果1的实现

</>部分:(只有一个div)

<div class='dash-ring'></div>

CSS部分:

.dash-ring{
        width:10px;
        height:10px;
        margin:0 auto;                    /*居中*/
        padding:10px;
        border:5px dashed #666;
        border-radius: 50%;                 /*设定圆角边框,50%即以当前元素宽度的一半为半径,画1/4圆,4个角拼起来就是一个圆*/
        animation: dash-ring-loading 1.5s infinite;   /*设定动画*/
        animation-timing-function: linear;        /*设定动画函数为线性(默认是ease,效果不同)*/
    }
@keyframes dash-ring-loading{                /*定义一个名为dash-ring-loading的动画*/
        50% {                          /*动画时长的百分比(参数范围:0%~100%),其中0%可用from代替,100%可用to代替。*/
            transform: rotate(180deg);           /*50%表示运行到一半,transform转至180度即可*/
        }
        100% {
            transform: rotate(360deg);          /*100%表示运行结束,转至360度回到原点*/
        }
    }
 

效果2的实现

</>部分:

<div class="m-load"></div>

<div class="m-load2">
    <div class="line">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>
    <div class="circlebg"></div>
</div>

CSS部分:

*{margin:0;padding:0;}
body{background:#000;}

.m-load,.m-load2{width:36px;height:36px;margin:100px auto;}
.m-load{background:url('load-v1.gif') #000 center center no-repeat;}
.m-load2{background:#000;}

/** 加载动画的静态样式 **/
.m-load2{position:relative;}
.m-load2 .line div{position:absolute;left:16px;top:0;width:3px;height:36px;}
.m-load2 .line div:before,.m-load2 .line div:after{content:'';display:block;height:50%;background:#fcfcfc;border-radius:5px;}
.m-load2 .line div:nth-child(2){-webkit-transform:rotate(30deg);}
.m-load2 .line div:nth-child(3){-webkit-transform:rotate(60deg);}
.m-load2 .line div:nth-child(4){-webkit-transform:rotate(90deg);}
.m-load2 .line div:nth-child(5){-webkit-transform:rotate(120deg);}
.m-load2 .line div:nth-child(6){-webkit-transform:rotate(150deg);}
.m-load2 .circlebg{position:absolute;left:50%;top:50%;width:18px;height:18px;margin:-9px 0 0 -9px;background:#000;border-radius:18px;}

/** 加载动画 **/
@keyframes load{
    0%{opacity:0;}
    100%{opacity:1;}
}
.m-load2 .line div:nth-child(1):before{-webkit-animation:load 1.2s linear 0s infinite;}
.m-load2 .line div:nth-child(2):before{-webkit-animation:load 1.2s linear 0.1s infinite;}
.m-load2 .line div:nth-child(3):before{-webkit-animation:load 1.2s linear 0.2s infinite;}
.m-load2 .line div:nth-child(4):before{-webkit-animation:load 1.2s linear 0.3s infinite;}
.m-load2 .line div:nth-child(5):before{-webkit-animation:load 1.2s linear 0.4s infinite;}
.m-load2 .line div:nth-child(6):before{-webkit-animation:load 1.2s linear 0.5s infinite;}
.m-load2 .line div:nth-child(1):after{-webkit-animation:load 1.2s linear 0.6s infinite;}
.m-load2 .line div:nth-child(2):after{-webkit-animation:load 1.2s linear 0.7s infinite;}
.m-load2 .line div:nth-child(3):after{-webkit-animation:load 1.2s linear 0.8s infinite;}
.m-load2 .line div:nth-child(4):after{-webkit-animation:load 1.2s linear 0.9s infinite;}
.m-load2 .line div:nth-child(5):after{-webkit-animation:load 1.2s linear 1s infinite;}
.m-load2 .line div:nth-child(6):after{-webkit-animation:load 1.2s linear 1.1s infinite;}
原文地址:https://www.cnblogs.com/cc1997/p/10446896.html