使用SVG+JS实现环形进度条

示例运行效果

整体思路:
1.使用SVG绘制圆环,以及内部百分比的文字
2.设置圆环的偏移属性,使其默认长度从0开始
3.通过定时器,每隔一段时间修改偏移属性,改变圆环长度,并修改文字的数值

<!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>
        .circle{
            fill: none;
            stroke:#4E9EED;
            stroke-width:8;
            /* 进度条填充一整个环形 */
            stroke-dasharray:502;
            /* dashoffset 为偏移量 正数为向逆时针偏移 负数为向顺时针位置偏移
				初始设为502时,进度条展示出的长度为0
			 */
            stroke-dashoffset:502; 
            stroke-linecap: round;
            /* 开口向上 */
            transform: rotate(-90deg);
            transform-origin: center;
            transform-box:fill-box;
        }
    </style>
</head>
<body>
    <svg width="300" height="300">
        <text class="text" x="100" y="100" style="text-anchor:middle">0%</text>
        <circle class="circle" cx="100" cy="100" r="80" ></circle>
    </svg>
    <script>
    	// 百分比初始值
        let p = 0;
        // 每50ms百分比+1
        let t = setInterval(()=>{
            setDegree()(p++)
        },50)
  
        function setDegree(){
        	// 获取所操作的元素
            let circleProgressBar = document.querySelector('.circle')
            let text = document.querySelector('.text')
            // 采用闭包方式,避免每次操作时重新获取dom
            return function(percent){
                if(percent<=100){
                	// 502为圆环的周长 502为一圈对应100% 由此计算传入percent时的值
                    circleProgressBar.style.strokeDashoffset = 502-(502/100)*percent
                    text.innerHTML = percent+'%'
                }
            }
        }
    </script>
</body>
</html>
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用以下 HTML、CSS 和 SVG 代码来画一个简单的流程图模板: ```html <div style="position: relative; width: 400px; height: 200px;"> <!-- 第一个矩形 --> <svg viewBox="0 0 100 100" style="position: absolute; left: 20px; top: 20px;"> <rect x="0" y="0" width="100" height="100" fill="#f00" /> <text x="50" y="60" font-size="16" text-anchor="middle" fill="#fff">步骤1</text> </svg> <!-- 第二个矩形 --> <svg viewBox="0 0 100 100" style="position: absolute; left: 150px; top: 20px;"> <rect x="0" y="0" width="100" height="100" fill="#0f0" /> <text x="50" y="60" font-size="16" text-anchor="middle" fill="#fff">步骤2</text> </svg> <!-- 第三个矩形 --> <svg viewBox="0 0 100 100" style="position: absolute; left: 280px; top: 20px;"> <rect x="0" y="0" width="100" height="100" fill="#00f" /> <text x="50" y="60" font-size="16" text-anchor="middle" fill="#fff">步骤3</text> </svg> <!-- 箭头线1 --> <svg viewBox="0 0 400 200" style="position: absolute; left: 0; top: 0;"> <line x1="70" y1="70" x2="150" y2="70" stroke="#000" stroke-width="2" marker-end="url(#arrow)" /> <defs> <marker id="arrow" markerWidth="10" markerHeight="10" refX="8" refY="5" orient="auto" markerUnits="strokeWidth"> <path d="M0,0 L0,10 L8,5 z" fill="#000" /> </marker> </defs> </svg> <!-- 箭头线2 --> <svg viewBox="0 0 400 200" style="position: absolute; left: 0; top: 0;"> <line x1="200" y1="70" x2="280" y2="70" stroke="#000" stroke-width="2" marker-end="url(#arrow)" /> <defs> <marker id="arrow" markerWidth="10" markerHeight="10" refX="8" refY="5" orient="auto" markerUnits="strokeWidth"> <path d="M0,0 L0,10 L8,5 z" fill="#000" /> </marker> </defs> </svg> </div> ``` 其中,`div` 元素用来创建包裹 SVG 元素的容器,`position: relative;` 属性用来指定容器的定位方式,`width` 和 `height` 属性用来指定容器的大小。每个 SVG 元素用来创建矩形或箭头线,`viewBox` 属性用来指定 SVG 的视口大小和位置,`style` 属性用来指定 SVG 元素的定位方式和位置。矩形内部使用了 `rect` 元素和 `text` 元素,`x` 和 `y` 属性用来指定文本的位置,`font-size` 属性用来指定文本的大小,`text-anchor` 属性用来指定文本的对齐方式。箭头线用 `line` 元素来创建,`x1` 和 `y1` 属性指定了线条的起点坐标,`x2` 和 `y2` 属性指定了线条的终点坐标,`stroke` 属性指定了线条的颜色,`stroke-width` 属性指定了线条的宽度,`marker-end` 属性指定了箭头的位置和方向。箭头的形状定义方式与前面的示例相同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值