Sub topic 10
Listening for transitionend and animationend
This chapter assumes you know how to write CSS transitions and @keyframes animations, and that you can select elements and add event listeners in JavaScript. CSS handles the motion. JavaScript events tell you when the motion has finished, so you can clean up, chain steps, or start other work.
The events at a glance
Both transitions and animations fire events on the element. They bubble, so you can listen on a parent.
| Event | Fires when | Fires per |
|---|---|---|
| `transitionrun` | A transition is created, before any delay | Property |
| `transitionstart` | The delay ends and the transition begins | Property |
| `transitionend` | A transition completes | Property |
| `transitioncancel` | A transition is interrupted | Property |
| `animationstart` | The animation begins, after any delay | Animation |
| `animationiteration` | Each iteration ends, except the last | Animation |
| `animationend` | The animation completes | Animation |
| `animationcancel` | The animation is removed before it finishes | Animation |
The key detail is "per property". A transition on background-color and transform fires two transitionend events. Each event has a propertyName and an elapsedTime in seconds.
Filtering transitionend by property
Because the event fires once per finished property, a handler without a check can run more than once. Use event.propertyName to react to one property.
const button = document.querySelector('.button');
button.addEventListener('transitionend', (event) => {
if (event.propertyName === 'transform') {
console.log('Transform transition finished');
}
});Bubbling adds a second trap. A transition on a child also triggers the parent's listener. Check event.target === event.currentTarget when you only care about the element itself.
Reacting to animationend
For animations, the event carries animationName, which is the name from @keyframes. This matters when an element runs several animations at once.
const card = document.querySelector('.card');
card.addEventListener('animationend', (event) => {
if (event.animationName === 'fade-in') {
card.classList.remove('is-entering');
}
});A common use is removing a class that exists only to start an animation. With fill-mode: both, the final keyframe still applies after the animation ends, so removing the class may make the element jump back. Plan the cleanup so the resting styles match the last keyframe.
When no event fires
Events are not guaranteed to arrive. Know these cases:
- Zero duration. A transition with
0sduration and no delay never starts, so no event fires. - No value change. If the old and new values are equal, or the property is not animatable, nothing runs.
display: none. If the element stops rendering, running transitions are cancelled and you gettransitioncancel, nottransitionend.- Interrupted transitions. Hovering off a button mid-transition reverses it. The first transition is cancelled and a new one starts.
animation-iteration-count: infinite.animationendnever fires. Useanimationiterationinstead.
If your logic must run no matter what, listen for both the end and cancel events, or use a fallback timer.
function onFinished(element, name, callback) {
const done = (event) => {
if (event.animationName !== name) return;
element.removeEventListener('animationend', done);
element.removeEventListener('animationcancel', done);
callback();
};
element.addEventListener('animationend', done);
element.addEventListener('animationcancel', done);
}
onFinished(document.querySelector('.card'), 'fade-in', () => {
console.log('Entry animation is over or was cancelled');
});Common errors
- Running a handler several times because the transition covers multiple properties and no
propertyNamecheck exists. - Reacting to events that bubbled up from child elements.
- Waiting for
animationendon an infinite animation. - Forgetting that a hidden element or a reduced-motion rule can remove the event.
Exercise
A button transitions both background-color and transform over 200ms. Your transitionend handler logs a message, and you see it twice per hover. Why, and how do you fix it?
Show answer
transitionend fires once for each property that finishes, so two properties give two events. Fix it by checking event.propertyName (for example === 'transform') inside the handler so it only reacts to the property you care about.
Exercise
Why will an animationend listener never run for .badge { animation: pulse 1.5s infinite; }, and which event can you use instead?
Show answer
An infinite animation never completes, so animationend never fires. Use animationiteration, which fires at the end of each iteration.