Home > Web Front-end > CSS Tutorial > How Can I Detect the Completion of CSS Transitions?

How Can I Detect the Completion of CSS Transitions?

Mary-Kate Olsen
Release: 2024-11-28 00:12:10
Original
745 people have browsed it

How Can I Detect the Completion of CSS Transitions?

Callbacks on CSS Transitions

Query: Can one receive a notification when a CSS transition has been completed?

Answer: In browsers that support callbacks, an event is triggered upon transition completion. The event varies among browsers:

  • Webkit (Chrome, Safari): webkitTransitionEnd
  • Firefox: transitionend
  • IE9 : msTransitionEnd
  • Opera: oTransitionEnd

WebKit TransitionEnd Issue

Beware that webkitTransitionEnd may not always trigger. This occurs when the animation has no visible effect on the element. To work around this, consider a timeout to fire the event handler if the event is not triggered as expected.

Recommended Code Structure

var transitionEndEventName = "XXX"; // Specify the appropriate event name
var elemToAnimate = ... // Element to be animated
var done = false;
var transitionEnded = function() {
    done = true;
    // Code to execute when transition finishes
    elemToAnimate.removeEventListener(transitionEndEventName, transitionEnded, false);
};
elemToAnimate.addEventListener(transitionEndEventName, transitionEnded, false);

// Animation triggering code here

// Fallback for browsers without event notifications
setTimeout(function() {
    if (!done) {
        transitionEnded();
    }
}, XXX); // Calculate XXX based on animation time and grace period
Copy after login

Note: Refer to "How do I normalize CSS3 Transition functions across browsers?" for normalizing transition event names.

The above is the detailed content of How Can I Detect the Completion of CSS Transitions?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template