【问题标题】:Simple svg css progress circle简单的 svg css 进度圈
【发布时间】:2021-04-08 02:45:38
【问题描述】:

我正在尝试寻找一种方法来实现没有动画的简单进度圈(静态)。我发现的示例具有非常不同的百分比偏移量,如下例所示。如何使我的进度圈以这样的方式进行,如果我提供 50% 的偏移量,那么它正好是 50%(半填充)?

.u-absoluteCenter {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
}
.u-flexCenter {
  display: flex;
  align-items: center;
  justify-content: center;
}
.u-offscreen {
  position: absolute;
  left: -999em;
}
.demo {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
  display: flex;
  align-items: center;
  justify-content: center;
}
.progress {
  transform: rotate(-90deg);
}
.progress__value {
  stroke-dasharray: 0;
  stroke-dashoffset: 0;
}
@-webkit-keyframes progress {
  from {
    stroke-dashoffset: 339.292;
  }
  to {
    stroke-dashoffset: 0;
  }
}
@keyframes progress {
  from {
    stroke-dashoffset: 339.292;
  }
  to {
    stroke-dashoffset: 0;
  }
}
<svg width="120" height="120" viewBox="0 0 120 120">
    <circle cx="60" cy="60" r="54" fill="none" stroke="#e6e6e6" stroke-width="12" />
    <circle cx="60" cy="60" r="54" fill="none" stroke="#f77a52" stroke-width="12"
        stroke-dasharray="339.292" stroke-dashoffset="339.292" />
</svg>

【问题讨论】:

  • 对于 25% 尝试stroke-dasharray="84.823 254.469",其中笔划 84.823 代表路径长度 (339.292) 的 25%,间隙 (254.469) 代表 75%。

标签: html css svg


【解决方案1】:

您可以利用 SVG 属性来设置路径长度,而不必计算它。

pathLength 将长度设置为您需要的任何值...例如 100 表示进度条。

pathLength 属性允许作者指定路径的总长度,以用户单位为单位。然后使用该值来校准浏览器的距离计算与作者的距离计算,通过使用比率 pathLength/(路径长度的计算值)缩放所有距离计算。

pathLength="100"

然后您也可以将stroke-dasharray 设置为100,然后根据需要调整stroke-dashoffset....

::root {
  --val: 0;
}

svg {
  transform: rotate(-90deg);
}

.percent {
  stroke-dasharray: 100;
  stroke-dashoffset: calc(100 - var(--val));
}

.fifty {
  --val: 50;
}

.sixty {
  --val: 60;
}

.ninety {
  --val: 90;
}
<svg width="120" height="120" viewBox="0 0 120 120">
    <circle cx="60" cy="60" r="54" fill="none" stroke="#e6e6e6" stroke-width="12" />
    <circle class="percent fifty" cx="60" cy="60" r="54" fill="none" stroke="#f77a52" stroke-width="12" pathLength="100" />
</svg>

<svg width="120" height="120" viewBox="0 0 120 120">
    <circle cx="60" cy="60" r="54" fill="none" stroke="#e6e6e6" stroke-width="12" />
    <circle class="percent sixty" cx="60" cy="60" r="54" fill="none" stroke="#f77a52" stroke-width="12" pathLength="100" />
</svg>


<svg width="120" height="120" viewBox="0 0 120 120">
    <circle cx="60" cy="60" r="54" fill="none" stroke="#e6e6e6" stroke-width="12" />
    <circle class="percent ninety" cx="60" cy="60" r="54" fill="none" stroke="#f77a52" stroke-width="12" pathLength="100" />
</svg>

【讨论】:

  • 但这仅给出 3 个值(50、60、90),我认为 OP 需要 0 - 100 之间的任何值。这适用吗?
  • 这些值仅用于演示目的。调整值很简单
  • 啊,好吧。你能解释一下如何从 HTML 代码中提供一个值吗?喜欢 26%
  • 像这样@Saif codepen.io/andywebo/pen/gORyOYQ ...只需在内联样式属性中进行计算。
  • 刚刚注意到 calc(100 - x) 值在移动 Safari 和移动 Chrome(在 iOS 上)的 stroke-dashoffset 中不起作用。必须改用结果数字。
【解决方案2】:

正如 Paulie 所说,pathLength 是进步圈子的关键

现代的自定义元素(所有现代浏览器都支持)使得 HTML 元素可重用

 <svg-progress-circle percent="30"></svg-progress-circle>
 <svg-progress-circle percent="20" color="blue"></svg-progress-circle>
 <svg-progress-circle percent="80" color="gold"></svg-progress-circle>

为交互式演示添加了范围输入。

百分比是元素的一个属性,你可以使用如下代码设置:

    document.getElementById("Slider1").percent = <PERCENTAGE>;

如果您不想要虚线灰色全圆,请从 pathLenght=120 path 中删除虚线设置

我使用了path 而不是重叠circles,因为使用其他一些设置几乎相同的代码可以创建饼图。

<style>
  svg { width: 150px; background: teal }
  svg-progress-circle[percent="100"] path { stroke: green }
</style>

<svg-progress-circle percent="30"></svg-progress-circle>
<svg-progress-circle percent="20" color="blue"></svg-progress-circle>
<svg-progress-circle percent="80" color="gold"></svg-progress-circle>

<script>
  customElements.define("svg-progress-circle", class extends HTMLElement {
    connectedCallback() {
      let d = 'M5,30a25,25,0,1,1,50,0a25,25,0,1,1,-50,0'; // circle
      this.innerHTML = 
      `<input type="range" min="0" max="100" step="10" value="30"`+ // delete 2 lines
      ` oninput="this.parentNode.percent=this.value" /><br>`+ // just for demo
      
      `<svg viewBox="0 0 60 60">
       <path stroke-dasharray="10 2"   stroke-dashoffset="-19" 
             pathlength="120" d="${d}" fill="grey" stroke="lightgrey" stroke-width="5"/>
       <path stroke-dasharray="30 70" stroke-dashoffset="-25" 
             pathlength="100" d="${d}" fill="none" 
             stroke="${this.getAttribute("color")||"red"}" stroke-width="5"/>
       <text x="50%" y="57%" text-anchor="middle">30%</text></svg>`;
       
      this.style.display='inline-block';
      this.percent = this.getAttribute("percent");
    }
    set percent(val = 0) {
      this.setAttribute("percent", val);
      let dash = val + " " + (100 - val);
      this.querySelector("path+path").setAttribute('stroke-dasharray', dash);
      this.querySelector("text").innerHTML = val + "%";
      this.querySelector("input").value = val;
    }
  })
</script>

注意:我正在开发一个完整的 Web 组件,它可以显示饼图和精美的进度圈,例如:

但是,它是众多副项目之一... HTML 示例和混淆源代码可在 https://pie-meister.github.io/

【讨论】:

    猜你喜欢
    • 2012-12-22
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 2015-01-03
    • 2015-06-16
    • 2021-02-19
    相关资源
    最近更新 更多