CSS - 实现loading动画 - 拖尾效果

812 阅读1分钟

实现效果

loading.png

实现思路

  • 圆弧,通过两个内外嵌套的圆构建(loading-border & loading-content
  • 使用背景渐变色实现拖尾效果(:after - 在元素后面添加内容)
  • 拖尾最前方的圆形是通过圆圈定位到固定位置实现的(:before - 构建伪元素,在元素前面添加内容)

实现代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        .container {
            width: 100%;
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-top: 100px;
        }
        .loading {
            width: 100px;
            height: 100px;
            border-radius: 50%;
            position: relative;
            overflow: hidden;
            padding: 10px;
        }
        .loading-content {
            width: 100%;
            height: 100%;
            background-color: #fff;
            position: relative;
            z-index: 9;
            border-radius: 50%;
        }
        .loading-border {
            width: 100%;
            height: 100%;
            position: absolute;
            top: 0;
            left: 0;
            z-index: 1;
            border-radius: 50%;
            background-color: #f0f1f6;
        }
        .loading-mask {
            width: 100%;
            height: 100%;
            position: absolute;
            top: 0;
            left: 0;
            z-index: 2;
            animation: rotate 2s linear infinite;
            /* -moz-animation: Firefox;  */
            /* -webkit-animation: Safari & Chrome; */
            /* -o-animation: Opera; */
        }
        .loading-mask::before {
            content: '';
            display: block;
            position: absolute;
            width: 10px;
            height: 10px;
            background-color: #6c8de5;
            left: calc(50% - 5px);
            bottom: 0;
            border-radius: 50%;
        }
        .loading-mask::after {
            content: '';
            display: block;
            position: absolute;
            background-image: linear-gradient(45deg, #6c8de5, #f0f1f6);
            width: 50%;
            height: 60%;
            bottom: 0%;
            left: 50%;
        }

        @keyframes rotate {
            from {
                transform: rotate(0);
            }
            to {
                transform: rotate(360deg);
            }
        }

        /* @-moz-keyframes */
        /* @-webkit-keyframes */
        /* @-o-keyframes */
    </style>
</head>
<body>
    <div class="container">
        <div class="loading">
            <div class="loading-mask"></div>
            <div class="loading-border"></div>
            <div class="loading-content"></div>
        </div>
    </div>
</body>
</html>