本文记录通过 HTML + CSS + 部分原生 JS 使用 SVG 嵌入 HTML 文档的用法实现常见的圆形和矩形进度条效果,效果图如下:(实际运行效果是进度条从 0 过渡到一个目标值比如 100%)


下面直接上代码:
HTML:线性渐变色的代码可以不加,非必须
- <svg class="box" width="200" height="200">
-
- <defs>
- <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
- <stop offset="0%" style="stop-color:rgb(57, 242, 218);stop-opacity:1" />
- <stop offset="50%" style="stop-color:rgb(6, 166, 230);stop-opacity:1" />
- <stop offset="100%" style="stop-color:hsl(223, 92%, 50%);stop-opacity:1" />
- linearGradient>
- defs>
-
- <circle class="progress" cx="100" cy="100" r="60" fill="transparent" stroke="url(#grad1)" stroke-width="10">circle>
-
- <text class="text" x="100" y="100" fill="#0c4ef5" text-anchor="middle" alignment-baseline="middle">text>
- svg>
CSS:仅处理了 svg 的位置而已,整个 CSS 都非必须,可以不写
- .box {
- position: absolute;
- top: 0;
- left: 0;
- right: 0;
- bottom: 0;
- margin: auto;
- }
- /* 对进度条这里用到的最重要的两个属性说明 */
- .progress {
- /* 注:使用 document.querySelector('.progress').getTotalLength() 可以得到圆一圈大小约为377 */
- /* stroke-dasharray: 377; */
- /* stroke-dashoffset 表示当前进度条剩余长度
- 为 0 时表示进度条已完成,最大为 stroke-dasharray 的值,等于 stroke-dasharray 的值 时表示进度为 0
- 我们可以使用 js 动态更新这个值得到进度逐步增加的效果
- */
- /* stroke-dashoffset: 377; */
- }
JavaScript:主要实现进度条“跑起来”的效果,原理就是动态更新圆形的 stroke-dashoffset 值
- // 获取进度条元素
- const progress = document.querySelector('.progress')
- const text = document.querySelector('.text') // 获取内部的文本元素
- // 获取圆一整圈的长度
- const maxLen = Math.ceil(progress.getTotalLength()) // 结果可向上取整
- progress.style.strokeDasharray = maxLen
- console.log(maxLen);
-
- // 效果1:自定义进度 teep 条
- // 定义的进度效果列表(注:表示的是百分比的值)
- /* const loadings = [5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
- let loadingIndex = 0
- const timer = setInterval(() => {
- progress.style.strokeDashoffset = maxLen - (loadings[loadingIndex] / 100 * maxLen)
- loadingIndex += 1
- text.innerHTML = `${loadings[loadingIndex]}%`
- // 进度列表的值取完就停止定时器
- if(loadingIndex === loadings.length) {
- clearInterval(timer)
- text.innerHTML = `OK`
- }
- }, 200); // 注:间隔时间越短进度条越丝滑
- */
-
- // 效果2:匀速从 0~100% 的进度效果
- let num = maxLen // 进度条的初始值,效果为进度条为 0
- // 此类场景使用 window.requestAnimationFrame() 进行循环比使用定时器性能更好
- let timer = window.requestAnimationFrame( function fn() {
- // 循环继续的条件
- if(num > 0) {
- num -= 2 // 减得越小动画持续时间越长
- progress.style.strokeDashoffset = num
- text.innerHTML = ((1 - num / maxLen) * 100).toFixed() + '%'
- // 继续循环则递归调用 fn 函数
- timer = window.requestAnimationFrame(fn)
- } else {
- // 循环停止
- progress.style.strokeDashoffset = 0
- // 清除定时器
- window.cancelAnimationFrame(timer)
- }
- })
-
HTML:线性渐变色的代码可以不加,非必须
- <svg class="box" width="800" height="20">
-
- <defs>
- <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
- <stop offset="0%" style="stop-color:rgb(57, 242, 218);stop-opacity:1" />
- <stop offset="50%" style="stop-color:rgb(6, 166, 230);stop-opacity:1" />
- <stop offset="100%" style="stop-color:rgb(12, 78, 245);stop-opacity:1" />
- linearGradient>
- defs>
-
- <rect class="rect" width="0" height="20" rx="5" ry="5" fill="url(#grad1)">rect>
- <text class="text" x="400" y="12" fill="red" text-anchor="middle" alignment-baseline="middle">0%text>
- svg>
CSS:核心代码是给 box 加一个背景色和内部矩形同样的圆角
- .box {
- position: absolute;
- top: 40px;
- left: 50%;
- transform: translateX(-50%);
- background-color: #eee;
- border-radius: 5px;
- }
-
JavaScript:核心原理就是动态给矩形的 width 赋值可以百分比
- const rect = document.querySelector('.rect')
- const text = document.querySelector('.text')
- let rectWidth = 80 // 矩形的宽度,即停止动画的值 单位: 百分比
- let loadingWidth = 0 // 进度条开始时的初始值
- // 此类场景使用 window.requestAnimationFrame() 进行循环比使用定时器性能更好
- let timer = window.requestAnimationFrame( function fn() {
- // 循环继续的条件
- if(loadingWidth < rectWidth) {
- loadingWidth += 0.5 // 加的越小动画持续时间越长
- rect.style.width = loadingWidth + '%'
- text.innerHTML = loadingWidth.toFixed() + '%'
- // 继续循环则递归调用 fn 函数
- timer = window.requestAnimationFrame(fn)
- } else {
- // 循环停止
- rect.style.width = rectWidth + '%'
- // 清除定时器
- window.cancelAnimationFrame(timer)
- }
- })
-
以上就是使用 HTML + CSS +少量的原生 JS 实现圆形、矩形进度条效果的示例代码了
源码地址:progress-demo: 通过 HTML + CSS + 部分原生 JS 使用 SVG 嵌入 HTML 文档的用法实现常见的圆形和矩形进度条效果项目源码
更多关于 SVG 的教程:菜鸟教程-SVG教程