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("http://www.w3.org/2000/svg","circle"); circle.setAttribute("r", "5"); circle.setAttribute("fill", "red"); var ani = document.createElementNS("http://www.w3.org/2000/svg","animateMotion"); ani.setAttribute("dur", "26s"); ani.setAttribute("repeatCount", "indefinite"); ani.setAttribute("rotate", "auto"); var mpath = document.createElementNS("http://www.w3.org/2000/svg","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 答疑

评论 抢沙发

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

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

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏