CSS环形进度条

在这里插入图片描述

	<div class="percent">
        <svg>
            <circle cx="70" cy="70" r="70" />
            <circle id="circle" cx="70" cy="70" r="70" />
        </svg>
        <div class="number">
            <h3>
                <span id="percent">20</span>
                <span>%</span>
            </h3>
        </div>
    </div>

    <script type="text/javascript">
        // 动态设置百分比
        const percentBox = document.querySelector("#percent")
        const circle = document.querySelector("#circle")
        let percent = 20
        //每隔1.5s进度条累加一个随机百分比
        const timer = setInterval(() => {
            percent += Math.ceil(Math.random() * 30)
            if (percent > 100) {
                percent = 100
                clearInterval(timer)
            }
            //设置样式
            percentBox.innerHTML = percent
            circle.style.strokeDashoffset = `calc(440 - 440 * (${percent} / 100))`
        }, 1500
        )
    </script>
	.percent {
        position: relative;
        width: 150px;
        height: 150px;
        svg {
            width: 150px;
            height: 150px;
            circle {
                fill: none;
                stroke-width: 10;
                transform: translate(5px, 5px);
                /*设置虚线长度=圆的周长(2*80*3.14)*/
                stroke-dasharray: 440;
                /* 设置偏移量 */
                stroke-dashoffset: 440;
                &:nth-child(1) {
                    stroke-dashoffset: 0;
                    stroke: #f3f3f3;
                }
                &:nth-child(2) {
                    /* 设置偏移量,显示百分比进度20% */
                    stroke-dashoffset: calc(440 - 440 * (20/100));
                    stroke: darkorchid;
                }
            }
        }
        .number {
            position: absolute;
            left: 0;
            top: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            width: 100%;
            height: 100%;
            color: #333;
            span {
                &:nth-child(1) {
                    font-size: 40px;
                }
                &:nth-child(2) {
                    font-size: 20px;
                }
            }
        }
    }

相关推荐

  1. css系列:进度

    2024-05-14 08:42:09       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-14 08:42:09       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-14 08:42:09       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-14 08:42:09       82 阅读
  4. Python语言-面向对象

    2024-05-14 08:42:09       91 阅读

热门阅读

  1. Jupyter集成AI环境搭建@miniconda@FreeBSD

    2024-05-14 08:42:09       30 阅读
  2. git之从整个版本中彻底删除文件)

    2024-05-14 08:42:09       31 阅读
  3. 十大排序算法

    2024-05-14 08:42:09       36 阅读
  4. 混合使用MFC与QT的深度技术分析

    2024-05-14 08:42:09       30 阅读
  5. Oracle中TEMPORARY tablespace和PERMANENT tablespace的差别

    2024-05-14 08:42:09       32 阅读
  6. Excel中`SUM`和`SUMPRODUCT`

    2024-05-14 08:42:09       30 阅读
  7. 驱动开发-用户空间和内核空间数据传输

    2024-05-14 08:42:09       34 阅读
  8. Elasticsearch映射定义

    2024-05-14 08:42:09       31 阅读
  9. RSA非对称加密解密,前端公钥加密后端私钥解密

    2024-05-14 08:42:09       31 阅读
  10. Spring Cloud 架构技术版本选型

    2024-05-14 08:42:09       35 阅读
  11. 优化运行效率

    2024-05-14 08:42:09       32 阅读