首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何通过在svg中启动新循环来停止先前的动画

如何通过在svg中启动新循环来停止先前的动画
EN

Stack Overflow用户
提问于 2019-05-23 01:29:15
回答 1查看 152关注 0票数 1

我有6个对象要设置动画。动画从加载页面的第一个圆圈开始,然后一个接一个地继续。重点是,它应该在任何时候通过单击任何圆圈重新启动,应该从那个圆圈开始,并且应该由右侧的下一个圆圈继续。问题是,在点击之后,前一个循环不会停止,并继续播放上一个和新的循环。我应该向代码中添加什么才能使其正常工作?

我尝试添加不同的"end“属性值,但都不起作用

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 23.0.1, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 640 480" enable-background="new 0 0 640 480" xml:space="preserve">

<defs>
  	<animate id="1p_anim" xlink:href="#_1p-circle" attributeName="stroke-dashoffset" begin="0s; click" values="600; 0" 
	restart="always"
 	dur="2s" repeatCount="1"  
 	fill="remove"  
 	calcMode="linear" 
 	stroke-opacity="1"
	end="1p_anim.click; 2p_anim.click; 3p_anim.click"
 	/>
 	<animate id="2p_anim" xlink:href="#_2p-circle" attributeName="stroke-dashoffset" begin="1p_anim.end; click" values="600; 0" 
	restart="always"
 	dur="1s" repeatCount="1"  
 	fill="remove"  
 	calcMode="linear" 
 	stroke-opacity="1"
	end="1p_anim.click; 2p_anim.click; 3p_anim.click"
 	/>
 	<animate id="3p_anim" xlink:href="#_3p-circle" attributeName="stroke-dashoffset" begin="2p_anim.end; click" values="600; 0" 
	restart="always"
 	dur="2s" repeatCount="1"  
 	fill="remove"  
 	calcMode="linear" 
 	stroke-opacity="1"
	end="1p_anim.click; 2p_anim.click; 3p_anim.click"
 	/>
 	<animate id="4p_anim" xlink:href="#_4p-circle" attributeName="stroke-dashoffset" begin="3p_anim.end; click" values="600; 0" 
	restart="always"
 	dur="2s" repeatCount="1"  
 	fill="remove"  
 	calcMode="linear" 
 	stroke-opacity="1"
 	end="1p_anim.click; 2p_anim.click; 3p_anim.click"
	/>
 	<animate id="5p_anim" xlink:href="#_5p-circle" attributeName="stroke-dashoffset" begin="4p_anim.end; click" values="600; 0" 
	restart="always"
 	dur="2s" repeatCount="1"  
 	fill="remove"  
 	calcMode="linear" 
 	stroke-opacity="1"
 	end="1p_anim.click; 2p_anim.click; 3p_anim.click"
	/>
 	<animate id="6p_anim" xlink:href="#_6p-circle" attributeName="stroke-dashoffset" begin="5p_anim.end; click" values="600; 0" 
	restart="always"
 	dur="2s" repeatCount="1"  
 	fill="remove"  
 	calcMode="linear" 
 	stroke-opacity="1"
 	end="1p_anim.click; 2p_anim.click; 3p_anim.click"
	/>
</defs>
<g id="1p">
	<circle id="_1p-circle" stroke-dasharray= "600" stroke-dashoffset="0" stroke="#000" fill="#fff" cx="103.5" cy="194.5" r="20"/>
</g>
<g id="2p">
	<circle id="_2p-circle" stroke-dasharray= "600" stroke-dashoffset="0" stroke="#000" fill="#FFFFFF" cx="158.5" cy="194.5" r="20"/>
</g>
<g id="3p">
	<circle id="_3p-circle" stroke-dasharray= "600" stroke-dashoffset="0" stroke="#000" fill="#FFFFFF" cx="210.5" cy="194.5" r="20"/>
</g>
<g id="4p">
	<circle id="_4p-circle" stroke-dasharray= "600" stroke-dashoffset="0" stroke="#000" fill="#FFFFFF" cx="262.5" cy="194.5" r="20"/>
</g>
<g id="5p">
	<circle id="_5p-circle" stroke-dasharray= "600" stroke-dashoffset="0" stroke="#000" fill="#FFFFFF" cx="314.5" cy="194.5" r="20"/>
</g>
<g id="6p">
	<circle id="_6p-circle" stroke-dasharray= "600" stroke-dashoffset="0" stroke="#000" fill="#FFFFFF" cx="366.5" cy="194.5" r="20"/>
</g>
</svg>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-24 23:10:42

我认为这个用例对于标准动画功能来说有点太复杂了。

我建议只使用一点Javascript。

代码语言:javascript
运行
复制
var circleElements = document.querySelectorAll("svg circle");
var animateElements = document.querySelectorAll("svg animate");
var currentIndex = -1;
var timeoutHandle = null;

circleElements.forEach(function(elem, index) {
  // Add a click event listener to all the circles 
  elem.addEventListener("click", function(evt) {
    circleClick(index);
  });
});


function circleClick(index) {
  // A circle has been clicked on
  currentIndex = index;
  // Trigger the animation of this circle
  animateElements.item(currentIndex).beginElement();
  // Clear any timeout that is running
  clearTimeout(timeoutHandle);
  // Start a new timeout which, when it expires, will trigger the animation on the next circle
  timeoutHandle = setTimeout(function() {
    nextCircle();
  }, 1000);
}

function nextCircle() {
  // We have been called by a timeout.
  // Incement the index
  currentIndex++;
  if (currentIndex < animateElements.length) {
    // If we are not yet at the last circle, trigger the animation on the new circle
    animateElements.item(currentIndex).beginElement();
    // Start a timeout for the circle after this one.
    timeoutHandle = setTimeout(function() {
      nextCircle();
    }, 1000);
  }
}

// Start animating right away with the first circle
circleClick(0);
代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 23.0.1, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 640 480" enable-background="new 0 0 640 480" xml:space="preserve">

<defs>
  	<animate id="1p_anim" xlink:href="#_1p-circle" attributeName="stroke-dashoffset" begin="indefinite" values="600; 0" 
	restart="always"
 	dur="2s" repeatCount="1"  
 	fill="remove"  
 	calcMode="linear" 
 	stroke-opacity="1"
 	/>
 	<animate id="2p_anim" xlink:href="#_2p-circle" attributeName="stroke-dashoffset" begin="indefinite" values="600; 0" 
	restart="always"
 	dur="1s" repeatCount="1"  
 	fill="remove"  
 	calcMode="linear" 
 	stroke-opacity="1"
 	/>
 	<animate id="3p_anim" xlink:href="#_3p-circle" attributeName="stroke-dashoffset" begin="indefinite" values="600; 0" 
	restart="always"
 	dur="2s" repeatCount="1"  
 	fill="remove"  
 	calcMode="linear" 
 	stroke-opacity="1"
 	/>
 	<animate id="4p_anim" xlink:href="#_4p-circle" attributeName="stroke-dashoffset" begin="indefinite" values="600; 0" 
	restart="always"
 	dur="2s" repeatCount="1"  
 	fill="remove"  
 	calcMode="linear" 
 	stroke-opacity="1"
	/>
 	<animate id="5p_anim" xlink:href="#_5p-circle" attributeName="stroke-dashoffset" begin="indefinite" values="600; 0" 
	restart="always"
 	dur="2s" repeatCount="1"  
 	fill="remove"  
 	calcMode="linear" 
 	stroke-opacity="1"
	/>
 	<animate id="6p_anim" xlink:href="#_6p-circle" attributeName="stroke-dashoffset" begin="indefinite" values="600; 0" 
	restart="always"
 	dur="2s" repeatCount="1"  
 	fill="remove"  
 	calcMode="linear" 
 	stroke-opacity="1"
	/>
</defs>
<g id="1p">
	<circle id="_1p-circle" stroke-dasharray= "600" stroke-dashoffset="0" stroke="#000" fill="#fff" cx="103.5" cy="194.5" r="20"/>
</g>
<g id="2p">
	<circle id="_2p-circle" stroke-dasharray= "600" stroke-dashoffset="0" stroke="#000" fill="#FFFFFF" cx="158.5" cy="194.5" r="20"/>
</g>
<g id="3p">
	<circle id="_3p-circle" stroke-dasharray= "600" stroke-dashoffset="0" stroke="#000" fill="#FFFFFF" cx="210.5" cy="194.5" r="20"/>
</g>
<g id="4p">
	<circle id="_4p-circle" stroke-dasharray= "600" stroke-dashoffset="0" stroke="#000" fill="#FFFFFF" cx="262.5" cy="194.5" r="20"/>
</g>
<g id="5p">
	<circle id="_5p-circle" stroke-dasharray= "600" stroke-dashoffset="0" stroke="#000" fill="#FFFFFF" cx="314.5" cy="194.5" r="20"/>
</g>
<g id="6p">
	<circle id="_6p-circle" stroke-dasharray= "600" stroke-dashoffset="0" stroke="#000" fill="#FFFFFF" cx="366.5" cy="194.5" r="20"/>
</g>
</svg>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56262093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档