繁体   English   中英

如何动态更改svg线的宽度

[英]how to change the width of a svg line dynamically

我写了下面的代码,成功地增加了svg行的大小,但是却没有减小它的大小。 以下是代码:

 var x = function(a,s){ var line = document.getElementById('l1'); if (s > line.getAttribute(a) ) { // if si greater for (var i = line.getAttribute(a); i<=s;i++ ){ line.setAttribute(a, i); } }; if(s < line.getAttribute(a)){ for (var i = line.getAttribute(a); i<s;i-- ){ line.setAttribute(a, i); } }; }; x('x2', 400); // changes the width of the svg x('x2', 40); // fail 
 <html> <body> <svg heigth="210" width="500" id="svg1" > <line id="l1" x1="0" y1="50" x2="100" y2="50" style="stroke:rgb(250,30,0); stroke-width:2"/> </svg> </body> </html> 

如您所见,它可以增加宽度但不能减小宽度?

我在i<si--的第二个if语句中感到困惑,我认为存在所有错误(也许不是)

如果您能解释您的答案,那将非常好,这样它不仅对我有用,而且对其他人也有用。

谢谢!

这样可行

if(s < line.getAttribute(a)){
                for (var i = line.getAttribute(a); i > s;i-- ){ //changes here
                line.setAttribute(a, i);
                }
        };

http://jsfiddle.net/4xnpn3wj/

暂无
暂无

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

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