admin管理员组文章数量:1323176
<svg width="5cm" height="3cm" viewBox="0 0 500 300">
<path id="path1" d="M100,250 C 100,50 400,50 400,250"
fill="none" stroke="blue" stroke-width="7.06" />
<circle r="17.64" fill="red">
<animateMotion dur="6s" repeatCount="1" rotate="auto" >
<mpath xlink:href="#path1"/>
</animateMotion>
</circle>
</svg>
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);
<svg width="5cm" height="3cm" viewBox="0 0 500 300">
<path id="path1" d="M100,250 C 100,50 400,50 400,250"
fill="none" stroke="blue" stroke-width="7.06" />
<circle r="17.64" fill="red">
<animateMotion dur="6s" repeatCount="1" rotate="auto" >
<mpath xlink:href="#path1"/>
</animateMotion>
</circle>
</svg>
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/2000/svg","circle");
circle.setAttribute("r", "5");
circle.setAttribute("fill", "red");
var ani = document.createElementNS("http://www.w3/2000/svg","animateMotion");
ani.setAttribute("dur", "26s");
ani.setAttribute("repeatCount", "indefinite");
ani.setAttribute("rotate", "auto");
var mpath = document.createElementNS("http://www.w3/2000/svg","mpath");
mpath.setAttribute("xlink:href", "#path1");
ani.appendChild(mpath);
circle.appendChild(ani);
svg.append(circle);
Share
Improve this question
edited Mar 12, 2012 at 19:53
Stewbob
16.9k9 gold badges66 silver badges109 bronze badges
asked Mar 12, 2012 at 2:13
denceydencey
1,06117 silver badges25 bronze badges
1
- 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. – nak Commented Mar 12, 2012 at 2:54
2 Answers
Reset to default 7Use setAttributeNS
on "mpath" instead of setAttribute
.
mpath.setAttributeNS("http://www.w3/1999/xlink", "href", "#path1");
Here's a demo: http://jsfiddle/zh553/
Agree with using setAttributeNS
on "mpath" instead of setAttribute
.
However at least for Chrome (and other WebKit based browsers?) it seems to be necessary to pay attention to http://www.w3/TR/DOM-Level-2-Core/core.html#ID-ElSetAttrNS where it says the second parameter is the qualifiedName, the qualified name of the attribute to create or alter.
mpath.setAttributeNS("http://www.w3/1999/xlink", "xlink:href", "#path1");
Or, if needed, more generically:
mpath.setAttributeNS("http://www.w3/1999/xlink",
mpath.lookupPrefix("http://www.w3/1999/xlink") + ":href",
"#path1");
Also discussed at https://bugs.webkit/show_bug.cgi?id=22958
本文标签: How to add an animated svg via javascriptStack Overflow
版权声明:本文标题:How to add an animated svg via javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742133861a2422283.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论