纯CSS实现加载动画

先上效果图

效果图

通过一个父div和两个子div即可实现,主要考虑三点

  1. 两个子div重叠在一起

    设置父元素position: relative,然后设置子元素position: absolute就可以使子元素的div重合,然后配合left: calc(20% - 3px)使两个子元素的div居中

  2. divborder的设置

    使用border-radius: 50%使之成为圆形,然后使用border-top-color: transparent造成一个缺口

  3. 设置两个子div的动画或者父div的动画

    通过CSS的动画属性设置即可,可以使用贝塞尔曲线设置两个不同的animation-timing-function,当然只设置父元素的动画也是可以的

代码如下所示:

<!DOCTYPE html>
<head>
    <style>
        @keyframes anim {
            from { transform: rotate(0deg); }
            to { transform: rotate(360deg); }
        }
        .loadingBox {
            width: 80px;
            height: 80px;
            padding: 0px;
            left: calc(50% - 80px);
            top: calc(50% - 80px);
            position: absolute;
        }
        .outerBox {
            left: calc(10% - 3px);
            top: calc(10% - 3px);
            width: 80%;
            height: 80%;
            position: absolute;
            border-radius: 50%;
            border: medium cornflowerblue solid;
            border-right-color: transparent;
            animation: anim 847ms cubic-bezier(.42, .61, .58, .41) infinite;
        }
        .innerBox {
            left: calc(20% - 3px);
            top: calc(20% - 3px);
            width: 60%;
            height: 60%;
            position: absolute;
            border-radius: 50%;
            border: 3px cornflowerblue solid;
            border-top-color: transparent;
            animation: anim 971ms cubic-bezier(.42, .61, .58, .41) infinite;
        }
    </style>
</head>
<body style="padding: 20px;background-color: whitesmoke;">
    <div class="loadingBox" id="box">
        <div class="innerBox"></div>
        <div class="outerBox"></div>
    </div>
</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值