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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值