JavaScript Element animationcancel Event

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
DOM event
Instance event

What You’ll Learn

The animationcancel event fires when a CSS Animation aborts instead of finishing with animationend. Learn addEventListener vs onanimationcancel, AnimationEvent properties, common cancel triggers (hide element, change animation-name), and why MDN marks this event Limited availability.

01

Kind

Instance event

02

Type

AnimationEvent

03

When

Animation aborted

04

Handler

onanimationcancel

05

Pair with

animationend

06

Status

Limited availability

Introduction

CSS animations usually end with animationend. Sometimes they stop unexpectedly—for example you hide the element with display: none, or you change animation-name so the running animation is removed. In those cases the browser should fire animationcancel instead of animationend.

That makes animationcancel useful for cleanup: release state, reset UI labels, or log that the animation did not finish normally.

💡
Beginner tip

Prefer addEventListener("animationcancel", ...). Some Chromium builds historically supported the event via addEventListener but not the onanimationcancel property.

Understanding animationcancel

An instance event on elements that participate in CSS Animations. MDN: it fires when a CSS Animation unexpectedly aborts—any time it stops without sending animationend.

  • Aborts when the animation is removed or the node is hidden (directly or via an ancestor).
  • AnimationEvent — includes animationName, elapsedTime, pseudoElement.
  • Not the normal “animation finished” signal—that is animationend.
  • Limited availability — not Baseline; verify on your target browsers.

📝 Syntax

Use the event name with addEventListener, or set the handler property:

JavaScript
addEventListener("animationcancel", (event) => { });

onanimationcancel = (event) => { };

Event type

An AnimationEvent (inherits from Event).

Event properties

PropertyMeaning
animationNameThe animation-name that generated the animation
elapsedTimeSeconds the animation had been running (paused time excluded)
pseudoElement:: name if animated on a pseudo-element; otherwise ""

⚖️ Animation lifecycle events

EventWhen it firesTypical use
animationstartAnimation beginsMark UI as “playing”
animationiterationEach iteration ends (except the last)Count loops
animationendAnimation finishes normallyCleanup after success
animationcancelAnimation aborts earlyCleanup after interrupt

⚠️ Limited Availability (Not Baseline)

MDN labels animationcancel as Limited availability: it is not Baseline because support is incomplete in some widely used browsers. Prefer addEventListener over onanimationcancel for broader coverage.

It is still a standard CSS Animations event—not Deprecated, Experimental, or Non-standard. Always test cancel paths (hide, remove class, change animation-name) on your supported browsers.

⚡ Quick Reference

GoalCode
Listenel.addEventListener("animationcancel", fn)
Handler propertyel.onanimationcancel = fn
Trigger (hide)el.style.display = "none"
Trigger (remove)Clear/change animation-name or remove animating class
Read nameevent.animationName
Read timeevent.elapsedTime
MDN statusLimited availability (not Baseline)

🔍 At a Glance

Four facts to remember about animationcancel.

Event type
AnimationEvent

Name + time

Means
aborted

Not animationend

Common trigger
display:none

Or remove name

Baseline
no

Limited availability

Examples Gallery

Examples follow MDN Element: animationcancel event. Try It Yourself labs start a short CSS animation, then cancel it so you can see the event fire.

📚 Getting Started

Register handlers the MDN ways, then cancel by hiding.

Example 1 — addEventListener("animationcancel")

MDN: listen on an animating element, then set display: none to abort.

JavaScript
const animated = document.querySelector(".animated");

animated.addEventListener("animationcancel", () => {
  console.log("Animation canceled");
});

// Abort the running CSS animation (where supported):
animated.style.display = "none";
Try It Yourself

How It Works

Hiding the element interrupts the animation before a normal end, so the browser fires animationcancel instead of animationend.

Example 2 — onanimationcancel Property

MDN’s alternate style using the event handler property.

JavaScript
const animated = document.querySelector(".animated");

animated.onanimationcancel = () => {
  console.log("Animation canceled");
};

animated.style.display = "none";

// Tip: prefer addEventListener when onanimationcancel is missing.
Try It Yourself

How It Works

Same event, different registration API. If the property is undefined in your browser, fall back to addEventListener.

📈 Details, Remove & Lifecycle

Read AnimationEvent fields, cancel by removing the animation, watch the full set.

Example 3 — Read animationName and elapsedTime

Use AnimationEvent properties for logging or debugging.

JavaScript
const box = document.getElementById("box");

box.addEventListener("animationcancel", (event) => {
  console.log("canceled:", event.animationName);
  console.log("elapsedTime:", event.elapsedTime);
  console.log("pseudoElement:", JSON.stringify(event.pseudoElement));
});

// Start animation via CSS class, then abort after a moment:
box.classList.add("spin");
setTimeout(() => {
  box.style.display = "none";
}, 200);
Try It Yourself

How It Works

elapsedTime is how long the animation had been running (in seconds) when cancel fired—not including paused time.

Example 4 — Cancel by Removing the Animation

Change animation-name (or remove the class that applied it) so the animation is removed.

JavaScript
const box = document.getElementById("box");

box.addEventListener("animationcancel", () => {
  console.log("Animation canceled (name removed)");
});

box.classList.add("slide");
setTimeout(() => {
  // Removing the class clears animation-name → abort (where supported)
  box.classList.remove("slide");
}, 150);
Try It Yourself

How It Works

MDN lists changing animation-name so the animation is removed as a classic cancel path—same idea as toggling off an animating class mid-flight.

Example 5 — Full Lifecycle Log

Inspired by MDN’s live example: log start, iteration, end, and cancel together.

JavaScript
const el = document.querySelector("p.animation");
const log = document.getElementById("log");
const btn = document.getElementById("toggle");
let iterations = 0;

function append(msg) {
  log.textContent += msg + " ";
}

el.addEventListener("animationstart", () => append("start"));
el.addEventListener("animationiteration", () => {
  iterations += 1;
  append("iteration:" + iterations);
});
el.addEventListener("animationend", () => {
  append("end");
  el.classList.remove("active");
  btn.textContent = "Activate animation";
});
el.addEventListener("animationcancel", () => append("canceled"));

btn.addEventListener("click", () => {
  el.classList.toggle("active");
  log.textContent = "";
  iterations = 0;
  const on = el.classList.contains("active");
  btn.textContent = on ? "Cancel animation" : "Activate animation";
});
Try It Yourself

How It Works

Toggling the animating class off while the animation is running removes the animation and should log canceled. Letting it finish logs end instead.

🚀 Common Use Cases

  • Cleaning up UI state when a panel is hidden mid-animation.
  • Distinguishing “finished” vs “interrupted” animation analytics.
  • Resetting loading indicators if a route change aborts an entrance animation.
  • Teaching the full CSS animation event set alongside animationend.
  • Coordinating class toggles that both start and stop named animations.

🔧 How It Works

1

CSS animation is running

animationstart (and maybe iterations) already fired.

Play
2

Something aborts it

Hide the node, or remove/change animation-name.

Interrupt
3

Browser fires animationcancel

An AnimationEvent is dispatched (no animationend).

Event
4

Your handler cleans up

Log, reset classes, or update UI for an interrupted animation.

📝 Notes

  • MDN: Limited availability (not Baseline)—verify cancel behavior on target browsers.
  • Not Deprecated, Experimental, or Non-standard — no status banner required.
  • Prefer addEventListener over onanimationcancel for compatibility.
  • Hiding via an ancestor can also cancel animations on descendants.
  • Related learning: afterscriptexecute, addEventListener(), animate(), JavaScript hub.

Limited Browser Availability

animationcancel is a standard CSS Animations event, but MDN marks it Limited availability (not Baseline). Logos use the shared browser-image-sprite.png sprite from this project. Prefer addEventListener; some Chromium versions historically lacked onanimationcancel.

Limited availability · Not Baseline

Element animationcancel

Supported in modern Firefox, Safari (13.1+), and Chromium engines for the event itself—always verify cancel paths and handler-property support.

Limited Not Baseline
Google Chrome 83+ event · prefer addEventListener
Supported
Mozilla Firefox Supported since Firefox 54+
Full support
Apple Safari 13.1+ (earlier builds incomplete)
Supported
Microsoft Edge 83+ Chromium · prefer addEventListener
Supported
Opera 69+ · Chromium-based
Supported
Internet Explorer No animationcancel support
Unavailable
animationcancel Limited

Bottom line: Use addEventListener("animationcancel", …) and test hide / remove-animation cases on your supported browsers.

Conclusion

animationcancel tells you a CSS animation was interrupted instead of finishing with animationend. Listen with addEventListener, read AnimationEvent details when you need them, and remember support is not Baseline everywhere.

Continue with animationend, animate(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use addEventListener("animationcancel", ...)
  • Handle both animationend and animationcancel for cleanup
  • Test hide / class-remove cancel paths on real browsers
  • Log animationName when debugging multiple animations
  • Keep cancel handlers light and idempotent

❌ Don’t

  • Assume every animation stop fires animationend
  • Rely only on onanimationcancel in Chromium
  • Assume Baseline support without checking MDN
  • Confuse Web Animations API cancel with this CSS event
  • Forget ancestor display: none can cancel child animations

Key Takeaways

Knowledge Unlocked

Five things to remember about animationcancel

CSS animation aborted—cleanup without waiting for animationend.

5
Core concepts
🎬 02

AnimationEvent

name + time

API
👁️ 03

Hide cancels

display:none

Trigger
🔔 04

addEventListener

preferred

Listen
⚠️ 05

Not Baseline

limited

Compat

❓ Frequently Asked Questions

It fires when a CSS Animation stops unexpectedly — that is, it stops running without sending an animationend event. Common causes include changing animation-name so the animation is removed, or hiding the element (or an ancestor) with CSS such as display: none.
No. MDN does not mark Element animationcancel as Deprecated, Experimental, or Non-standard. It is a standard CSS Animations event, but MDN labels it Limited availability (not Baseline) because support is missing or incomplete in some browsers.
An AnimationEvent. Useful read-only properties include animationName, elapsedTime, and pseudoElement.
Prefer element.addEventListener("animationcancel", handler). You can also set onanimationcancel where supported. In some Chromium versions the handler property was missing even though addEventListener worked — so addEventListener is the safer choice.
animationend fires when an animation finishes its intended run (including after the last iteration). animationcancel fires when the animation is aborted early and never reaches a normal end.
Yes in supporting browsers: setting display: none on the animating element (or a containing node that hides it) can abort the animation and fire animationcancel instead of animationend.
Did you know?

If an animation runs on a pseudo-element such as ::before, event.pseudoElement starts with :: (for example ::before). For animations on the element itself, it is an empty string.

Next: animationend

Learn when a CSS animation finishes its intended run.

animationend →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful