[英]animationend event not firing
I am trying to add an animationend
event to an element, but the event doesn't get fired. 我正在尝试向一个元素添加一个
animationend
事件,但事件不会被触发。 What am I doing wrong, and how can I fix it? 我做错了什么,我该如何解决?
var btn = document.getElementById('btn'); var elem = document.getElementById('elem'); var timeOutFunc; btn.addEventListener('click', function() { elem.classList.add('show'); clearTimeout(timeOutFunc); timeOutFunc = setTimeout(function() { elem.classList.remove('show') }, 1000); }); elem.addEventListener('animationend', function(e) { console.log('animation ended'); });
#elem { background-color: orange; width: 100px; height: 100px; opacity: 0; transition: opacity 500ms ease; } #elem.show { opacity: 1; transition: none; }
<button id="btn">Press Me</button> <div id="elem"></div>
There are two separate animating events. 有两个单独的动画事件。
When using the css transition
use transitionend
, and when using @keyframes/animation
, use animationend
. 使用css
transition
使用transitionend
时,使用@keyframes/animation
,请使用animationend
。
You need to modify the animation style property of the element this is your updated example at Jsfiddle 您需要修改元素的动画样式属性,这是您在Jsfiddle中更新的示例
#elem {
background-color: orange;
width: 100px;
height: 100px;
opacity: 0;
transition: opacity 500ms ease;
}
/* Chrome, Safari, Opera */
@-webkit-keyframes myopacity {
0% { opacity: 0; }
100% { opacity: 1; }
}
@keyframes myopacity {
0% { opacity: 0; }
100% { opacity: 1; }
}
#elem.show {
WebkitAnimation : myopacity 1s 1;
animation : myopacity 1s 1;
}
var btn = document.getElementById('btn');
var elem = document.getElementById('elem');
var timeOutFunc;
btn.addEventListener('click', function() {
elem.classList.add('show');
/* clearTimeout(timeOutFunc);
timeOutFunc = setTimeout(function() {
elem.classList.remove('show')
}, 1000);*/
});
elem.addEventListener('animationend', function(e) {
console.log('');
alert('animation ended');
elem.classList.remove('show')
});
<button id="btn">Press Me</button>
<div id="elem"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.