问题
Trying to make an svg rectangle that moves when button pushed. Right now I just want the x to be modified by a function.
function modX()
{
document.getElementById("rectangle").transform = 'translate(295 115)';
}
var x = 20;
var y = 20;
modX();
<svg width="1000" height="1000" >
<rect id="rectangle" x="0" y="20" width="100" height="100"
style="fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)"></rect>
</svg>
I'm fairly new to code so please avoid css or jquery.
回答1:
you can change its x by using javaScript document.getElementById("rectangle").setAttribute('x', X_value)
set the X_value
to the value you want it to change.
来源:https://stackoverflow.com/questions/49456517/change-position-of-svg-element-using-js