How to add an animated svg via javascript?

If I write the svg in plain html/svg file, it works fine, the circle animates correctly. But if I add the circle element dynamically via javascript, circle was added, but it didn't animate. What's wrong? js code: var svg = $("svg"); //use jquery var circle = document.createElementNS("","circle"); circle.setAttribute("r", "5"); circle.setAttribute("fill", "red"); var ani = document.createElementNS("","animateMotion"); ani.setAttribute("dur", "26s"); ani.setAttribute("repeatCount", "indefinite"); ani.setAttribute("rotate", "auto"); var mpath = document.createElementNS("","mpath"); mpath.setAttribute("xlink:href", "#path1"); ani.appendChild(mpath); circle.appendChild(ani); svg.append(circle);
Can you post the full HTML file instead of that snippet? I don't know what JS framework you're using to select the SVG. And it would be nice to know if you're putting SVG markup inline or embedding it with an embed tag.

以上就是How to add an animated svg via javascript?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准