Change Position of svg element using JS

旧城冷巷雨未停 提交于 2021-02-05 04:52:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!