javascript - 动画结束监听器无法正常工作

标签 javascript css animation event-listener

我的时间栏有一个 CSS 动画,带有 id="progress-bar" 和动画类 .anim。 动画是 CSS:

@-webkit-keyframes myfirst {
  from {
    width: 100%;
    background-color: #86e01e;
  }
  to {
    width: 0%;
    background-color: #f63a0f;
  }
}

.anim {
-webkit-animation: myfirst 5s linear;
-webkit-animation-fill-mode: forwards;
}

我正在使用函数 reset() 重新启动动画

function reset() {
document.getElementById("progress-bar").className = "";
setTimeout(function(){document.getElementById("progress-bar").className = "anim";},1);
}

现在我希望在动画结束时执行函数 lose()。 我也尝试过使用 animationend 监听器进行多种操作,但它对我不起作用...

var x = document.getElementById("progress-bar");
x.addEventListener('webkitAnimationEnd', function() { 
lose(); //whatever i type here doesn't work
}, false);

有什么想法吗?

最佳答案

试试这个作为修复:

x.addEventListener('animationend', lose(), false);
x.addEventListener('webkitanimationend', lose(), false);

关于javascript - 动画结束监听器无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28304345/

相关文章:

javascript - 如何在 SVG 中创建嵌入阴影(透明背景)?

javascript - 使用 nuxt-child 仅一层的 Nuxt 动态嵌套路由

javascript - 我的 table 顶部有一个随机的反斜杠

html - 增加 Bootstrap 中搜索栏的宽度

c++ - 帧率独立动画问题

android - 如何以编程方式启动滑动以使用 RecyclerView 关闭

iphone - 如何用动画隐藏/显示 UISearchBar 范围栏?

javascript - 悬停在 MouseEnter 和 onMouseLeave React 上不起作用。超出最大调用堆栈大小

javascript - Chart.JS 格式标签中的日期

html - 具有多个元素的 CSS 文本溢出省略号