Course contentsShow
← Web Development
Lesson 742 of 2,98213. DOM EventsFree lesson

Animation and Transition Events

Detect when CSS animations and transitions start, end, or iterate with animationend and transitionend.

Animation and Transition Events

You'll learn how to detect when CSS animations and transitions begin, finish, or repeat using JavaScript event listeners.

When you apply CSS animations or transitions to elements, they don't just happen silently—the browser fires special events at key moments during their lifecycle. These events let you coordinate JavaScript logic with your visual effects, like triggering a confetti explosion when an animation completes or chaining multiple transitions together.

The Core Events

transitionend fires when a CSS transition finishes. If you transition multiple properties simultaneously, the event fires once for each property.

const box = document.querySelector('.box');

box.addEventListener('transitionend', (event) => {
  console.log(`${event.propertyName} transition completed`);
});

// Trigger a transition via class toggle
box.classList.add('move');

animationstart fires when a CSS animation begins playing.

animationend fires when an animation completes (after all iterations).

animationiteration fires each time an animation loop completes (except the final one).

const spinner = document.querySelector('.spinner');

spinner.addEventListener('animationiteration', () => {
  console.log('Spin cycle completed');
});

spinner.addEventListener('animationend', () => {
  console.log('Animation finished');
});

Event Properties

These events include useful properties like animationName, elapsedTime, and propertyName (for transitions), helping you identify exactly which effect triggered the event.

Think of It Like This

Imagine a microwave timer—it beeps when it starts, when each minute passes, and when it finishes. Animation events work the same way, letting your code "listen" for those moments.

Key Takeaway: Use transitionend, animationstart, animationend, and animationiteration events to run JavaScript code at precise moments during CSS animations and transitions.