Home >Web Front-end >CSS Tutorial >How Can I Detect the Completion of a CSS Transition?
Callback on CSS Transition
Question:
Is it possible to receive a notification when a CSS transition has completed?
Answer:
Yes, if supported by the browser, an event is triggered when the transition ends. However, the specific event varies across browsers:
Additional Considerations:
Code Example:
const transitionEndEventName = "XXX"; // Determine the appropriate event name const elemToAnimate = ...; // Element to animate let done = false; const transitionEnded = () => { done = true; // Transition finished actions elemToAnimate.removeEventListener(transitionEndEventName, transitionEnded, false); }; elemToAnimate.addEventListener(transitionEndEventName, transitionEnded, false); // Trigger animation setTimeout(() => { if (!done) { console.log("Timeout required to call transition ended."); transitionEnded(); } }, XXX); // Replace XXX with animation duration plus a grace period
Note:
The above is the detailed content of How Can I Detect the Completion of a CSS Transition?. For more information, please follow other related articles on the PHP Chinese website!