Home >Web Front-end >CSS Tutorial >How Can I Trigger Callbacks on CSS3 Animation Completion?
Callbacks for CSS3 Animations
CSS3 animations offer impressive animation effects, but lack the flexibility of implementing callbacks upon completion. This article explores a solution to this by leveraging event listeners.
Problem:
How can we trigger a callback function when a CSS3 animation finishes? While JavaScript animations support callbacks, CSS3 animations seem to lack similar capabilities.
Solution:
Although CSS3 animations do not directly provide callback mechanisms, they emit events that can be captured using event listeners. This allows us to execute callbacks when these events occur. Here are two methods:
1. Using jQuery:
$("#sun").bind('oanimationend animationend webkitAnimationEnd', function() { alert("fin") });
2. Using Pure JavaScript:
element.addEventListener("webkitAnimationEnd", callfunction,false); element.addEventListener("animationend", callfunction,false); element.addEventListener("oanimationend", callfunction,false);
These event listeners detect the animation's completion and trigger the specified callback function. This ensures that the callback is executed reliably upon animation completion.
Live Demo:
For a practical demonstration, refer to the live fiddle at http://jsfiddle.net/W3y7h/ to witness the callback functionality in action.
The above is the detailed content of How Can I Trigger Callbacks on CSS3 Animation Completion?. For more information, please follow other related articles on the PHP Chinese website!