Course contentsShow
← Web Development
Lesson 511 of 2,9829. CSS Transitions and AnimationsFree lesson

Transition Events in JavaScript

Listening for transitionend, transitionstart, and transitioncancel events in code.

Transition Events in JavaScript

You'll learn how to detect and respond when CSS transitions begin, end, or are interrupted using JavaScript event listeners.

What Are Transition Events?

When a CSS transition runs, the browser fires special events at key moments. These events let your JavaScript code "listen" for when transitions start, finish, or get canceled. Think of them like notifications your browser sends when something interesting happens during an animation.

There are three transition events you can monitor:

transitionend

Fires when a transition completes successfully. This is the most commonly used event—perfect for triggering the next action in a sequence or cleaning up after an animation.

transitionstart

Fires when a transition begins. Use this to coordinate other effects or track that an animation has kicked off.

transitioncancel

Fires when a transition is interrupted before completing—for example, if the element is hidden, removed from the DOM, or a new transition starts on the same property.

How to Use Them

You attach these events with addEventListener(), just like click or scroll events:

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

box.addEventListener('transitionend', function(event) {
  console.log('Transition finished on property: ' + event.propertyName);
});

box.addEventListener('transitionstart', function() {
  console.log('Transition started!');
});

box.addEventListener('transitioncancel', function() {
  console.log('Transition was canceled');
});

Each event object includes useful properties like propertyName (which CSS property transitioned) and elapsedTime (how long it ran).

Key Takeaway: Transition events (transitionend, transitionstart, transitioncancel) let JavaScript detect and respond to CSS transitions in real-time, enabling you to chain animations, update UI state, or handle interruptions gracefully.