javascript - svg animateMotion on end 事件监听器

标签 javascript html css svg

我的 html 中有 svg,其中有一条路径和一个沿着路径移动的圆圈(带有 animateMotion)。如何检测 animationMotion 何时结束以便我可以执行任务?另外,如何重置 animationMotion - 即将移动圆圈带回到行的开头。下面是我的 svg html 代码:

<svg id="cc-line" x="0px" y="0px" viewBox="0 -23 150 55" width="150px" height="55px" xml:space="preserve">
      <path fill="#2471A3" d="M0 0 l150 0" stroke="#2471A3" stroke-width="5"/>
      <circle id="cc-line-circle" cx="0" cy="0" r="8" fill="#2471A3">
        <animateMotion id="cc-line-motion" path="M0 0 l150 0"
 			begin= "0s" dur="6.0s" repeatCount="indefinite" rotate="auto" fill="freeze">
      </circle>
    </svg>

最佳答案

您可以利用 onrepeat 事件来检测动画周期何时完成,并在此时运行自定义逻辑/任务。 这可以通过 JavaScript 通过以下方式实现:

var animationElement = document.querySelector('#cc-line-motion');

animationElement.onrepeat = function() {

  // An animation cycle was completed
  console.log('Task run at end of animation cycle')  
}

这是在 Chrome 68 中测试的 - 请注意 this is an experimental feature所以谨慎使用

关于javascript - svg animateMotion on end 事件监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52032210/

相关文章:

javascript - 使用页面中的隐藏元素来存储数据

JavaScript 警报

javascript - 如何获取<p>标签中的搜索框值 - HTML,JS

javascript - ReactJS:选中另一个复选框时取消选中先前选中的复选框

php - jQuery:在隐藏的 Div 上实现 UI 自动完成

javascript - 无法从 Spring Boot 过滤器在 http header 中发送 "Unauthorized"

css - 动画 : Spin svg path on its own center inside a larger rect box

javascript - Fullcalendar -> 更改所选事件的颜色

html - Bootstrap、页眉/横幅、匹配宽度和定位到正文

css - 如何在一个div中包含一个div?