利用svg实现渐变仪表盘进度条
1 使用circle画圆环,利用animate实现动画效果,利用defs定义渐变色,stroke-dasharray 属性,这是一个用来控制路径虚线疏密程度的属性,其值是一组描述虚线的短划线与空白间隙长度的数列。例如,如果设置 stroke-dasharray="5 2",则路径将以 5 个像素的短划线和 2 个像素的空白间隙交替显示,其中第一个数控制短划线长度,第二个数控制空白间隙长度。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | <! DOCTYPE html> < html lang="en"> < head > < meta charset="UTF-8"> < meta name="viewport" content="width=device-width, initial-scale=1.0"> < title >Document</ title > < style > .progress-circle { width: 200px; height: 200px; --percent: 75; /* 百分数 */ --size: 180px; /* 尺寸大小 */ --border-width: 15px; /* 环形宽度(粗细) */ --color: #7856d7; /* 主色 */ --inactive-color: #ccc; /* 辅助色 */ width: var(--size); height: var(--size); transform: rotate(135deg); border-radius: 50%; } .progress-circle > circle { cx: calc(var(--size) / 2); cy: calc(var(--size) / 2); r: calc((var(--size) - var(--border-width)) / 2); fill: none; stroke-width: var(--border-width); stroke-linecap: round; } </ style > </ head > < body > < svg class="progress-circle"> < circle stroke="var(--inactive-color)" style="stroke-dasharray: calc( 2 * 3.1415 * (var(--size) - var(--border-width)) / 2 * (var(--percent) / 100) ), 1000"/> < circle stroke="url(#Gradient01)" style="stroke-dasharray: calc( 2 * 3.1415 * (var(--size) - var(--border-width)) / 2 * (var(--percent) / 100) ), 1000" > <!-- 388.575 stroke-dasharray 计算的值 --> < animate attributeName="stroke-dasharray" values="0;388.575;388.575" dur="2s" /> </ circle > </ svg > < svg xmlns="http://www.w3.org/2000/svg"> < defs > < linearGradient id="Gradient01"> < stop offset="20%" stop-color="#39F" /> < stop offset="90%" stop-color="#F3F" /> </ linearGradient > </ defs > </ svg > </ body > </ html > |
具体参见:https://zhuanlan.zhihu.com/p/662139481
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构