javascript - 为什么单击时的关键帧动画只触发一次?

标签 javascript jquery css css-animations

下面是我的代码,关键帧动画只在第一次点击时发生。有什么想法吗?

解决了,看下面我的回答

JQUERY:

$(".audioplayer-playpause").click(function(){
  $('.audioplayer-playpause').css({

    //for firefox
    "-moz-animation-name":"playPausePulse",
    "-moz-animation-duration":"0.1s",
    "-moz-animation-iteration-count":"1",
    "-moz-animation-fill-mode":"forwards",

    //for safari & chrome
    "-webkit-animation-name":"playPausePulse",
    "-webkit-animation-duration":"0.1s",
    "-webkit-animation-iteration-count":"1",
    "-webkit-animation-fill-mode":"forwards",

  });
});

CSS:

@-moz-keyframes playPausePulse /*--for webkit--*/{
    0%   {background-color: rgba(0, 0, 0, 0.05);}
    50%   {background-color: rgba(0, 0, 0, 0.1);}
    100%   {background-color: rgba(0, 0, 0, 0.05);}
}

@-webkit-keyframes playPausePulse /*--for webkit--*/{
    0%   {background-color: rgba(0, 0, 0, 0.05);}
    50%   {background-color: rgba(0, 0, 0, 0.1);}
    100%   {background-color: rgba(0, 0, 0, 0.05);}
}

这非常相关,但我访问了两个引用链接,但找不到有效的解决方案: run keyframes-animation on click more than once

如有任何帮助,我们将不胜感激。

谢谢。 托马斯。

最佳答案

我找到了一个解决方案:

我添加了一个监听器以在末尾为元素提供新的/重置动画

JQUERY

$(".audioplayer-playpause").click(function(){
  $('.audioplayer-playpause').css({

  //for firefox
  "-moz-animation-name":"playPausePulse",
  "-moz-animation-duration":"0.1s",
  "-moz-animation-iteration-count":"1",
  "-moz-animation-fill-mode":"forwards",

  //for safari & chrome
  "-webkit-animation-name":"playPausePulse",
  "-webkit-animation-duration":"0.1s",
  "-webkit-animation-iteration-count":"1",
  "-webkit-animation-fill-mode":"forwards",

  });
});

   $(".audioplayer-playpause").bind('oanimationend animationend webkitAnimationEnd', function() { 
        $(".audioplayer-playpause").css({
            '-moz-animation-name': 'playPausePulseReset',
            '-webkit-animation-name': 'playPausePulseReset',
        });
   });

CSS

@-moz-keyframes playPausePulse /*--for webkit--*/{
  0%   {background-color: rgba(0, 0, 0, 0.05);}
  50%   {background-color: rgba(0, 0, 0, 0.1);}
  100%   {background-color: rgba(0, 0, 0, 0.05);}
}

@-webkit-keyframes playPausePulse /*--for webkit--*/{
  0%   {background-color: rgba(0, 0, 0, 0.05);}
  50%   {background-color: rgba(0, 0, 0, 0.1);}
  100%   {background-color: rgba(0, 0, 0, 0.05);}
}

@-moz-keyframes playPausePulseReset /*--for webkit--*/{
  0%, 100% {
    background-color: rgba(0, 0, 0, 0.05);
  }
}

@-webkit-keyframes playPausePulseReset /*--for webkit--*/{
  0%, 100% {
    background-color: rgba(0, 0, 0, 0.05);
  }
}

关于javascript - 为什么单击时的关键帧动画只触发一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27490151/

相关文章:

php - 使用 jQuery 设置 session 变量(不通过 Post 或 Get 传递)

html - 具有较高 z 索引的 DIV 在单击包含 Silverlight 对象的具有较低 z 索引的 div 时会隐藏

html - 如何使用 CSS 并排放置两个元素

javascript - AngularJS 两种方式绑定(bind)返回未定义

javascript - 上传前 Android webview 中的图像预览不起作用

jquery - 第 n 个 child 。每第 4/5 个、第 9/10 个、第 14/15 个元素为目标

css - 自动化 meteor 中的 css 浏览器供应商前缀

javascript - 如何遍历一个js对象并记录遍历路径?

javascript - 异步 Jasmine 测试在操作之前不会调用 beforeEach()

javascript - 动画代码不起作用