jquery - 使用 jQuery 操作 SVG 路径长度

标签 jquery svg path

如何将蓝线(atm 100%)的长度调整为 70%?

fiddle http://jsfiddle.net/7FP3J/2/

HTML

<div id="p0">0%</div>
<div id="p100">100%</div>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> 
    <path fill="none" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);" stroke="#0074c4" d="M80,80 A80,80 0 1,180.61117636732928,100.45116865416591" stroke-width="3" id="test"/>
</svg>

CSS

#p0 {
  color: #0074c4;
  left: 60px;
  position: absolute;
  top: 72px;
}
#p100 {
  color: #0074c4;
  left: 240px;
  position: absolute;
  top: 144px;
}

JS

setCircleTo(70);

function setCircleTo(percent)
{
    // percent   
    // $('#test').attr('d','') 
}

最佳答案

最简单的方法是使用破折号数组技巧。

function setCircleTo(percent)
{
    var path = $('#test').get(0);
    var pathLen = path.getTotalLength();
    var adjustedLen = percent * pathLen / 100;
    path.setAttribute('stroke-dasharray', adjustedLen+' '+pathLen);
}

Fiddle here

关于jquery - 使用 jQuery 操作 SVG 路径长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24779990/

相关文章:

javascript - 通过 Javascript 访问 SVG 元素

linux - pathconf( ) linux 等效 windows API

html - 如何让背景图像/图像通过 CSS 拉伸(stretch)以填充容器?

java - 加载启动文件

linux:更改默认程序

javascript - 回调函数——括号的使用

jquery - Select2 插件 : Showing selected options outside the form element

jquery - 使用 Bootstrap 的响应图像

jquery - 在 Jquery 中将 div 移动到新子级中

css - MathJax SVG 对标题和 CSS 的 react 很奇怪