You need to enable JavaScript to run this app. 如何给现有渐变圆形SVG进度条添加移动箭头并支持双向转动
优惠活动
大模型
产品
解决方案
定价
更多

如何给现有渐变圆形SVG进度条添加移动箭头并支持双向转动

实现方案

核心改造逻辑

  • 箭头实现:通过SVG内置的<marker>标签定义符合要求的箭头样式,直接绑定到进度条路径的末端,会自动跟随进度移动
  • 逆时针支持:新增自定义类.reverse,通过反转旋转角度、调整stroke-dashoffset计算逻辑实现逆时针转动效果,使用时给对应进度条的<li>标签加该类即可

完整修改后代码

HTML代码

<!--  Container  -->
<ul class="progress">
    <!--  顺时针进度条  -->
    <li data-name="Item 1" data-percent="Item 1">
        <svg viewBox="-10 -10 220 220">
        <g fill="none" stroke-width="2" transform="translate(100,100)">
        <path d="M 0,-100 A 100,100 0 0,1 86.6,-50" stroke="url(#cl1)"/>
        <path d="M 86.6,-50 A 100,100 0 0,1 86.6,50" stroke="url(#cl2)"/>
        <path d="M 86.6,50 A 100,100 0 0,1 0,100" stroke="url(#cl3)"/>
        <path d="M 0,100 A 100,100 0 0,1 -86.6,50" stroke="url(#cl4)"/>
        <path d="M -86.6,50 A 100,100 0 0,1 -86.6,-50" stroke="url(#cl5)"/>
        <path d="M -86.6,-50 A 100,100 0 0,1 0,-100" stroke="url(#cl6)"/>
        </g>
        </svg>
        <svg viewBox="-10 -10 220 220">
        <path d="M200,100 C200,44.771525 155.228475,0 100,0 C44.771525,0 0,44.771525 0,100 C0,155.228475 44.771525,200 100,200 C155.228475" stroke-dashoffset="590"></path>
        </svg>
    </li>
    <!--  逆时针进度条  -->
    <li class="reverse" data-name="Item 2" data-percent="Item 2">
        <svg viewBox="-10 -10 220 220">
        <g fill="none" stroke-width="2" transform="translate(100,100)">
        <path d="M 0,-100 A 100,100 0 0,1 86.6,-50" stroke="url(#cl1)"/>
        <path d="M 86.6,-50 A 100,100 0 0,1 86.6,50" stroke="url(#cl2)"/>
        <path d="M 86.6,50 A 100,100 0 0,1 0,100" stroke="url(#cl3)"/>
        <path d="M 0,100 A 100,100 0 0,1 -86.6,50" stroke="url(#cl4)"/>
        <path d="M -86.6,50 A 100,100 0 0,1 -86.6,-50" stroke="url(#cl5)"/>
        <path d="M -86.6,-50 A 100,100 0 0,1 0,-100" stroke="url(#cl6)"/>
        </g>
        </svg>
        <svg viewBox="-10 -10 220 220">
        <path d="M200,100 C200,44.771525 155.228475,0 100,0 C44.771525,0 0,44.771525 0,100 C0,155.228475 44.771525,200 100,200 C155.228475" stroke-dashoffset="590"></path>
        </svg>
    </li>
</ul>
<!--  定义渐变和箭头  -->
<svg width="0" height="0">
<defs>
<!-- 箭头定义 -->
<marker id="progress-arrow" markerWidth="25" markerHeight="25" refX="12.5" refY="12.5" orient="auto">
  <path d="M12.5 2 L20 23 L12.5 18 L5 23 Z" fill="#18FFA9" stroke="none"/>
</marker>
<linearGradient id="cl1" gradientUnits="objectBoundingBox" x1="0" y1="0" x2="1" y2="1">
    <stop stop-color="#18FFA9"/>
    <stop offset="100%" stop-color="#18FFA9"/>
</linearGradient>
<linearGradient id="cl2" gradientUnits="objectBoundingBox" x1="0" y1="0" x2="0" y2="1">
    <stop stop-color="#18FFA9"/>
    <stop offset="100%" stop-color="#4FC0EB"/>
</linearGradient>
<linearGradient id="cl3" gradientUnits="objectBoundingBox" x1="1" y1="0" x2="0" y2="1">
    <stop stop-color="#4FC0EB"/>
    <stop offset="100%" stop-color="#FD8AE6"/>
</linearGradient>
<linearGradient id="cl4" gradientUnits="objectBoundingBox" x1="1" y1="1" x2="0" y2="0">
    <stop stop-color="#FD8AE6"/>
    <stop offset="100%" stop-color="#4FC0EB"/>
</linearGradient>
<linearGradient id="cl5" gradientUnits="objectBoundingBox" x1="0" y1="1" x2="0" y2="0">
    <stop stop-color="#4FC0EB"/>
    <stop offset="100%" stop-color="#18FFA9"/>
</linearGradient>
<linearGradient id="cl6" gradientUnits="objectBoundingBox" x1="0" y1="1" x2="1" y2="0">
    <stop stop-color="#18FFA9"/>
    <stop offset="100%" stop-color="#18FFA9"/>
</linearGradient>
</defs>
</svg>

CSS代码

html,
body {
  background-color: black;
  color: white;
}

@keyframes load {
  0% {
    stroke-dashoffset: 0;
  }
}
@keyframes load-reverse {
  0% {
    stroke-dashoffset: 0;
  }
}
.progress {
  position: relative;
  padding: 0;
  text-align: center;
  gap: 20px;
  display: flex;
}
.progress > li {
  display: inline-block;
  position: relative;
  text-align: center;
  font-size: 28px;
  line-height: 34px;
}
.progress > li:after {
    content: attr(data-percent);
    position: absolute;
    top: 40%;
    left: 0;
    right: 0;
    font-size: 28px;
    text-align: center;
    margin: auto;
    width: 100%;
    vertical-align: middle;
    display: block;
    text-transform: uppercase;
}
.progress svg {
  width: 409px;
  height: 409px;
}
.progress svg:nth-child(2) {
  position: absolute;
  left: 0;
  top: 0;
  transform: rotate(-90deg);
}
.progress svg:nth-child(2) path {
  fill: none;
  stroke-width: 25;
  stroke-dasharray: 629;
  stroke: black;
  opacity: 0.9;
  animation: load 1s forwards;
  marker-end: url(#progress-arrow);
}
/* 逆时针样式 */
.progress > li.reverse svg:nth-child(2) {
  transform: rotate(90deg) scaleX(-1);
}
.progress > li.reverse svg:nth-child(2) path {
  animation: load-reverse 1s forwards;
}

使用说明

  • 需要顺时针转动的进度条直接使用默认<li>标签即可
  • 需要逆时针转动的进度条给<li>添加reverse类即可
  • 进度值调整:修改对应path的stroke-dashoffset属性,取值范围0~629,数值越小进度越高
  • 箭头大小已和25px的进度条宽度匹配,如需调整进度条宽度同步修改marker的宽高参数即可

内容的提问来源于stack exchange,提问作者Lucas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.21 12:21:03