【发布时间】:2020-02-08 06:06:32
【问题描述】:
我想在 text 元素的内容发生变化时将其从 A 点动画(移动)到 B 点
SVG:
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="300 160 350 150" height="80px" width="100%" xml:space="preserve">
<text id="text_animated" x="422" y="280" fill="white" font-size="17">
<animate attributeName="y" from="150" to="250" begin="3s" dur="5.5s" repeatCount="1" fill="freeze"/>
</text>
<circle cx="422" cy="280" fill="red" r="5">
<animate id="animation_depth_circle" attributeName="cy" from="150" to="250" begin="indefinite" dur="1.5s" repeatCount="1" fill="freeze" onend="endAnimate()" />
</circle>
</svg>
JS:
var counter=0;
var test_t = setInterval(function(){
document.getElementById('text_animated').textContent = ""+counter;
if(counter===120){
clearInterval(test_t);
}
counter=counter+1;
},10);
我的目标是随着文本的变化移动文本(在圆圈内)。问题是 text 没有移动。只有圆圈会移动。
顺便说一句。我不能用
document.getElementById('text_animated').setAttribuite('y',something);
因为它与 SVG 动画不同步(如果出现网络瓶颈问题)。我正在使用 Chrome。
编辑:
我设法使用 dy 来移动我的文本:
<text x="422" y="280" fill="white" >
<animate attributeName="dy" from="0" to="250" dur="1.5s" repeatCount="indefinite"/>
</text>
问题是,如果我用我的 javascript 更改文本,它不会移动。所以要么改变要么移动。
【问题讨论】:
标签: javascript html svg svg-animate