简体   繁体   English

svg 上的 Progressbar.js 边框半径

[英]Progressbar.js border-radius on the svg

Is it possible to have a border-radius to the progressbarjs that is filled 50% just like the parent divs border-radius?是否可以像父 div 的边界半径一样为进度条设置 50% 的边界半径?

在此处输入图像描述

HTML HTML

<div class="pgbar" id="pgbar"></div>

CSS CSS

  .pgbar {
    height: 46px;
    margin: 0 auto 30px;
    background-color: #D8D8D8;
    border-radius: 23px;
    padding: 2px;
    svg {
      height: 100%;
      width: 100%;
      border-radius: 23px;
    }
  }

JS JS

var bar = new ProgressBar.Line(pgbar, {
    color: '#00AFD0',
    trailWidth: 1,
    svgStyle: {borderRadius: '23px'}
});

bar.animate(0.5);

The border-radius does not work on the leading/right edge of the loading SVG because the SVG has a width of 100%, so the border-radius is actually on the extreme left/right edges of the SVG and therefore will not clip the path, if the path is not 100% in width. border-radius在加载 SVG 的前/右边缘不起作用,因为 SVG 的宽度为 100%,因此边界半径实际上在 ZCD15A75C26008693B6 的最左/右边缘上路径,如果路径的宽度不是 100%。

To fix this, you can use a combination of stroke-linecap: round to round the edge of the <path> element's stroke, and set the strokeWidth of the progress element programmatically.要解决此问题,您可以使用组合stroke-linecap: round to round the <path>元素的笔画边缘,并以编程方式设置进度元素的 strokeWidth。 Based on the documentation, the native size of the SVG is always 100×strokeWidth , so you will need to scale the canvas by the aspect ratio of the DOM element to achieve a rounded shape. 根据文档,SVG 的原始大小始终为100×strokeWidth ,因此您需要通过 DOM 元素的纵横比缩放 canvas 以实现圆形。

 var width = pgbar.clientWidth; var height = pgbar.clientHeight; var bar = new ProgressBar.Line(pgbar, { color: '#00AFD0', strokeWidth: height / width * 100, svgStyle: { strokeLinecap: 'round' } }); bar.animate(0.5);
 .pgbar { height: 46px; margin: 0 auto 30px; background-color: #D8D8D8; border-radius: 23px; padding: 2px; }.pgbar svg { height: 100%; width: 100%; border-radius: 23px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/progressbar.js/1.1.0/progressbar.min.js"></script> <div class="pgbar" id="pgbar"></div>

A word of caution tho: if the progress bar element's width changes, which causes its aspect ratio to change, you will need to find a way to change the strokeWidth : not sure if the plugin's API actually supports that.请注意:如果进度条元素的宽度发生变化,从而导致其纵横比发生变化,您将需要找到一种方法来更改strokeWidth :不确定插件的 API 是否真的支持它。 If you need to accommodate this sort of flexibility, you might be better off just writing your own simple component to handle progressbar animation: remember that <progress> element is quite well supported .如果您需要适应这种灵活性,您最好编写自己的简单组件来处理进度条 animation:记住<progress>元素得到了很好的支持

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM