An event handler for the animationcancel event. This event is sent when a CSS Animation unexpectedly aborts (in other words, any time it stops running without sending an animationend event), such as when the animation-name is changed such that the animation is removed, or when the animating node is hidden—either directly or because any of its containing nodes are hidden)—using CSS.
var animCancelHandler = target.onanimationcancel;
target.onanimationcancel = Function
Value
A Function to be called when an animationcancel event occurs indicating that a CSS animation has begun on the target, where the target object is an HTML element (HTMLElement), document (Document), or window (Window). The function receives as input a single parameter: an AnimationEvent object describing the event which occurred.
HTML content
<divclass="main"><divid="box"onanimationcancel="handleCancelEvent(event);"><divid="text">Box</div></div></div><divclass="button"id="toggleBox">
Hide the Box
</div><preid="log"></pre>
Leaving out some bits of the CSS that don't matter for the discussion here, let's take a look at the styles for the box that we're animating. First is the box itself, with all its properties, including animation, defined. We go ahead and describe the animation in-place here because the animation is intended to begin as soon as the page loads, rather than based on an event.
Since the animation is described as taking place an infinite number of times, alternating direction each time, the box will glide back and forth between the two corners until stopped or the page is closed.
JavaScript content
Before we get to the animation code, we define a function which logs information to a box on the user's screen. We'll use this to show information about the events we receive. Note the use of AnimationEvent.animationName and AnimationEvent.elapsedTime to get information about the event which occurred.
functionlog(msg, event){let logBox = document.getElementById("log");
logBox.innerHTML += msg;if(event){
logBox.innerHTML +=" <code>"+ event.animationName +"</code> at time "+ event.elapsedTime.toFixed(2)+" seconds.";}
logBox.innerHTML +="\n";};
Then we set up the handleCancelEvent() function, which is called in response to the animationcancel event, as set up in the HTML above. All we do here is log information to the console, but you might find other use cases, such as starting a new animation or effect, or terminating some dependent operation.
Then we add a method to handle toggle display between "flex" and "none" and establish it as the handler for a click event on the "Hide/Show" the Box button:
document.getElementById('toggleBox').addEventListener('click',function(){if(box.style.display =="none"){
box.style.display ="flex";
document.getElementById("toggleBox").innerHTML ="Hide the box";}else{
box.style.display ="none";
document.getElementById("toggleBox").innerHTML ="Show the box";}});
Toggling the box to display: none has the effect of aborting its animation. In browsers that support animationcancel, the event is fired and this handler is called.
At this time, no major browser supports animationcancel.
Result
Assembled together, you get this:
If your browser supports animationcancel, hiding the box using the button will cause a message to be displayed about the event.
Thanks! Please check your inbox to confirm your subscription.
If you haven’t previously confirmed a subscription to a Mozilla-related newsletter you may have to do so. Please check your inbox or your spam filter for an email from us.