jquery - 悬停时暂停 css 动画,然后从点继续

标签 jquery html css animation svg

我正在尝试在悬停时添加 svg 路径动画,并在 mouseOut 上反转动画。

如果 mouseOut 发生在 mouseOn 动画完成之前,我该如何暂停动画,并从该关键帧反转它?

此外,如果动画正在反转,并且您将鼠标悬停在动画中间,我希望反转动画停止,并从该点向前播放。

Please see Fiddle as example

我还使用 jquery 向元素添加属性。我需要只使用 css 来完成这个吗?

$(document).ready(function() {
  $('svg').hover(function() {
    /* Stuff to do when the mouse enters the element */
    $(this).find('circle').fadeIn(100);
    $(this).find('circle').attr("class", "social-circle movingCirc");
    console.log('on');
  }, function() {
    /* Stuff to do when the mouse leaves the element */
    $(this).find('circle').attr("class", "social-circle removingCirc");
    $(this).find('circle').delay(1900).fadeOut(100);
    console.log("hover off");
  });
});

谢谢你的帮助

最佳答案

我不知道 stroke-dashoffset 动画“停止和反转”是否可以用纯 CSS 动画来完成,但这里有一个 fork of your fiddle使用 jquery animate 的解决方案.

这是javascript:

$(function() {
  var $svg = $('svg');
  var $circle = $('circle', $svg);

  $svg.hover(function() {
    /* Stuff to do when the mouse enters the element */
    $circle
      .stop()
      .animate({
        'stroke-dashoffset': 0
      }, 2000);
  }, function() {
    /* Stuff to do when the mouse leaves the element */
    $circle
      .stop()
      .animate({
        'stroke-dashoffset': 900
      }, 2000);
  });
});

关于jquery - 悬停时暂停 css 动画,然后从点继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34886574/

相关文章:

javascript - 如何强制停止 JavaScript 图像预加载序列(Ariel Flesler 的预加载器)?

javascript - Photoset-grid + lightbox 点击时打开错误的图像

html - 如何用div添加2个背景图片

javascript - 上下文处理器中定义的字典在不重新加载页面的情况下不会刷新?

jquery - 升级到 Kentico 8.1 后如何使自定义 jQuery 模块正常工作?

javascript - 带滚动的 Bootstrap 导航选项卡

java - 如何在输入流中找到什么?

javascript - 突出显示/设置文本区域格式

css - 为什么必须将 grid-area 属性设置为所有 html 元素以留下空单元格?

html - 文本位于单元格中间,不会向上移动