1. 程式人生 > 其它 >CSS實現loading效果

CSS實現loading效果

CSS實現loading效果

前言

使用animation動畫實現loading效果。

一、條狀loading

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title
>
<style> * { margin: 0; padding: 0; } html, body { width: 100%; height: 100%; } .loading { display: flex; justify-content: space-around; align-items
: center; width: 100px; height: 100px; margin: 100px auto; } .loading span { width: 10px; height: 100%; background-color: darkcyan; border-radius: 5px; animation: load 1s linear infinite; }
@keyframes load { 0%, 100% { height: 50%; background-color: cornsilk; } 50% { height: 100%; background-color: darkcyan; } } .loading :nth-child(2) { animation-delay: 0.2s; } .loading :nth-child(3) { animation-delay: 0.4s; } .loading :nth-child(4) { animation-delay: 0.6s; } .loading :nth-child(5) { animation-delay: 0.8s; } .loadEffect { width: 100px; height: 100px; position: relative; margin: 0 auto; margin-top: 100px; } .loadEffect span { display: inline-block; width: 16px; height: 16px; border-radius: 50%; background: lightgreen; position: absolute; -webkit-animation: load 1.04s ease infinite; } @-webkit-keyframes load { 0% { opacity: 1; } 100% { opacity: 0.2; } }
</style> </head> <body> <div class="loading"> <span></span> <span></span> <span></span> <span></span> <span></span> </div> </body> </html>

二、圓圈loading

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        html,
        body {
            width: 100%;
            height: 100%;
        }
        
        .loadEffect {
            position: relative;
            width: 100px;
            height: 100px;
            margin: 100px auto;
        }
        
        .loadEffect span {
            position: absolute;
            width: 20px;
            height: 20px;
            border-radius: 10px;
            background-color: darkgray;
            animation: load2 2s ease-in-out infinite;
        }
        
        .loadEffect span:nth-child(1) {
            top: 50%;
            left: 0;
            margin-top: -10px;
        }
        
        .loadEffect span:nth-child(2) {
            top: 15px;
            left: 15px;
            animation-delay: 0.25s;
        }
        
        .loadEffect span:nth-child(3) {
            top: 0;
            left: 50%;
            margin-left: -10px;
            animation-delay: 0.5s;
        }
        
        .loadEffect span:nth-child(4) {
            top: 15px;
            right: 15px;
            animation-delay: 0.75s;
        }
        
        .loadEffect span:nth-child(5) {
            top: 50%;
            right: 0;
            margin-top: -10px;
            animation-delay: 1s;
        }
        
        .loadEffect span:nth-child(6) {
            bottom: 15px;
            right: 15px;
            animation-delay: 1.25s;
        }
        
        .loadEffect span:nth-child(7) {
            bottom: 0;
            right: 50%;
            margin-right: -10px;
            animation-delay: 1.5s;
        }
        
        .loadEffect span:nth-child(8) {
            bottom: 15px;
            left: 15px;
            animation-delay: 1.75s;
        }
        
        @keyframes load2 {
            0% {
                transform: scale(1.2);
                opacity: 1;
            }
            100% {
                transform: scale(0.3);
                opacity: 0.2;
            }
        }
    </style>
</head>

<body>
    <div class="loadEffect">
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
    </div>
</body>

</html>

參考博文:https://www.cnblogs.com/jr1993/p/4622039.html