利用html + css + javascript 创建环形进度条

<!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>Circular prograss bar |Hu Guang</title>
    <style>
        @import url('https://fonts.googleapis.com/css?family=Poppins:400,500,600,700&display=swap');
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: "Poppins", sans-serif;
        }
        html,body {
            display: grid;
            height: 100%;
            text-align: center;
            place-items: center; /* 将div标签居中展示 */
            background: #dde6f0;
        }
        .circular {
            height: 100px;
            width: 100px;
            /* background: red; */
            position: relative;
            transform: scale(2); /* transform 属性向元素应用 2D 或 3D 转换。该属性允许我们对元素进行旋转、缩放、移动或倾斜。 */
        }
        .circular .inner {
            /* display: none; */
            position: absolute;
            z-index: 6;
            top: 50%;
            left: 50%;
            height: 80px;
            width: 80px;
            margin: -40px 0 0 -40px;
            background: #dde6f0;
            border-radius: 100%; /* 20%设置圆角边框 */
            box-shadow: 0 1px 0 rgba(0, 0, 0, 0.2); /* 对象选择器 {box-shadow:[投影方式] X轴偏移量 Y轴偏移量阴影模糊半径 阴影扩展半径 阴影颜色} */
        }
        .circular .numb {  /* 定义动态计时容器样式*/
            position: absolute;
            top: 50%;
            left: 50%;
            transform:translate(-50%,-50%);
            z-index: 10;
            font-size: 18px;
            font-weight: 500;
            color: #4158d0;
        }
        .circular .bar {
            position: absolute;
            height: 100%;
            width: 100%;
            background: #fff;
            -webkit-border-radius: 100%;
            clip: rect(0px, 100px, 100px, 50px); /* 剪裁绝对定位元素。唯一合法的形状值是:rect (top, right, bottom, left)  */

        }
        .circle .bar .progress {
            position: absolute;
            height: 100%;
            width: 100%;
            background: #fff;
            -webkit-border-radius: 100%;
            clip: rect(0px, 50px, 100px, 0px); /* 剪裁绝对定位元素。唯一合法的形状值是:rect (top, right, bottom, left) */
            background: #4158d0;

        }
        .circle .left .progress {
            z-index: 1;
            animation: left 4s linear both;
        }
        @keyframes left {
            100%{
                transform: rotate(180deg);
            }
        }
        .circle .right {
            z-index: 3;
            transform: rotate(180deg);
        }
        .circle .right .progress {
            animation: right 4s linear both;
            animation-delay: 4s;
        }
        @keyframes right {
            100%{
                transform: rotate(180deg);
            }
        }
    </style>
</head>
<body>
    <div class="circular">
        <div class="inner"></div>
        <div class="numb">0%</div> <!-- 定义数据计时器容器 -->
        <div class="circle">
            <div class="bar left">
                <div class="progress"></div>
            </div>
            <div class="bar right">
                <div class="progress"></div>
            </div>
        </div>
    </div>

    <script>
        const numb = document.querySelector('.numb');
        let counter = 0;
        setInterval("timer()",79)
        function timer(){
            if(counter == 100){
                clearInterval();
            }else{
                counter +=1;
                numb.textContent = counter + '%'
            }
        }
    </script>
</body>
</html>
手打不易,且看且学会。

彩蛋

更多推荐