Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

animationend event not firing

I am trying to add an animationend event to an element, but the event doesn't get fired. What am I doing wrong, and how can I fix it?

JSFiddle

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>
like image 797
Jessica Avatar asked Jan 08 '17 06:01

Jessica


People also ask

What is Animationend in JavaScript?

The animationend event is fired when a CSS Animation has completed. If the animation aborts before reaching completion, such as if the element is removed from the DOM or the animation is removed from the element, the animationend event is not fired.

How do I know when CSS animation is finished?

In JavaScript, we can call the callback function once the animation is done. But in CSS, there is no option to perform any action after the end of the transition/animation. Whenever the transition is finished, the transitionend event will be triggered. We can use this event to find the end of the transition.


1 Answers

There are two separate animating events.

  1. animationend
  2. transitionend

When using the css transition use transitionend, and when using @keyframes/animation, use animationend.

like image 176
Jessica Avatar answered Oct 06 '22 18:10

Jessica