javascript - SVG:动态添加动画元素

标签 javascript css svg svg-animate

<分区>

我尝试添加 animate元素动态到我的 path元素,它工作正常。我的 html:

<g id="3" class="cluster loop-node">
<path fill="#edf1f2" stroke="#028d35" d="M92,-16C92,-16 408,-16 408,-16 414,-16 420,-22 420,-28 420,-28 420,-88 420,-88 420,-94 414,-100 408,-100 408,-100 92,-100 92,-100 86,-100 80,-94 80,-88 80,-88 80,-28 80,-28 80,-22 86,-16 92,-16"></path>
<text text-anchor="start" x="213.5547" y="-79.4" font-family="Times,serif" font-size="14.00" fill="#000000">1:11</text>
<text text-anchor="start" x="243.8131" y="-79.4" font-family="Times,serif" font-size="14.00" fill="#000000"> (Loop)</text>
</g>

我添加我的 animate javascript 元素 ( '<animate attributeType="XML" attributeName="stroke-width" values="6;1;6;1" dur="2s" repeatCount="3"></animate>'):

$("#3").find("path").append('<animate attributeType="XML" attributeName="stroke-width" values="6;1;6;1" dur="2s" repeatCount="indefinite"></animate>')

在那之后 animate标记已正确添加:

<g id="3" class="cluster loop-node">
<path fill="#edf1f2" stroke="#028d35" d="M92,-16C92,-16 408,-16 408,-16 414,-16 420,-22 420,-28 420,-28 420,-88 420,-88 420,-94 414,-100 408,-100 408,-100 92,-100 92,-100 86,-100 80,-94 80,-88 80,-88 80,-28 80,-28 80,-22 86,-16 92,-16"><animate attributetype="XML" attributename="stroke-width" values="6;1;6;1" dur="2s" repeatcount="indefinite"></animate></path>
<text text-anchor="start" x="213.5547" y="-79.4" font-family="Times,serif" font-size="14.00" fill="#000000">1:11</text>
<text text-anchor="start" x="243.8131" y="-79.4" font-family="Times,serif" font-size="14.00" fill="#000000"> (Loop)</text>
</g>

问题 是当我以这种方式(以编程方式)插入时动画没有显示。如果我插入相同的 html从一开始,它就可以工作,但是当我想以编程方式插入它们时就不行了。 我想也许我应该重新加载 div ,但动画没有运行。 我的代码有什么问题?

最佳答案

你必须用.beginElement()开始你的动画 这不是 jquery 方法,因此您不能将它与 jquery 对象一起使用。改用 Dom 对象

var myanim=document.createElementNS("http://www.w3.org/2000/svg", 'animate');
myanim.setAttribute("id","myAnimation"); 
myanim.setAttribute("attributeType","XML"); 
myanim.setAttribute("attributeName","stroke-width"); 
myanim.setAttribute("values","6;1;6;1"); 
myanim.setAttribute("dur","2s"); 
myanim.setAttribute("repeatCount","3"); 
var myPath = document.getElementById("path1");

myPath.appendChild(myanim);

function myFunction() {
console.log('hello')
//$("#myAnimation").beginElement();
document.getElementById("myAnimation").beginElement();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="myFunction()">Click me</button>

<svg width="320" height="320" viewBox="0 0 320 320">
<path
            id="path1" fill="#edf1f2" stroke="#ff8d35"
            d="M160,100 Q200,100,200,300 Q100,300,100,200 Z">


                </path>

 </svg>

关于javascript - SVG:动态添加动画元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49245135/

相关文章:

javascript - 将 cURL 转换为 Parse.Cloud.httpRequest

javascript - 在数组内的所有元素之间插入值的功能方法

CSS 背景位置更改 - Chrome 错误

html - CSS 变换 - 旋转超过 -180 度时图像消失

javascript - SVG 的 Markdown 模拟

android - SVG 无法在 Android Studio 中正确转换为具有高视口(viewport)的 XML Drawable

css - 使用 css 反转 svg 图像?

javascript - 从 Ember View 中删除类名

html - CSS 网格模板列重复不超过 X 次

javascript - Firefox 中左侧的 tab 相当于什么