Home > Web Front-end > CSS Tutorial > How Can jQuery Detect the Completion of CSS3 Transitions and Animations?

How Can jQuery Detect the Completion of CSS3 Transitions and Animations?

Patricia Arquette
Release: 2024-12-17 10:15:26
Original
772 people have browsed it

How Can jQuery Detect the Completion of CSS3 Transitions and Animations?

Detecting the Completion of CSS3 Transitions and Animations with jQuery

In web development, it's often desirable to perform an action after a CSS transition or animation has completed. However, determining the exact end of these animations can be tricky. This article will demonstrate how to use jQuery to detect the completion of both transitions and animations, ensuring precise control over DOM manipulation.

Transitions

To detect the end of a CSS transition via jQuery, utilize the following syntax:

$("#someSelector").bind("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function(){ ... });
Copy after login

This binds an event handler to the specified element that triggers when any of the supported transition end events occur.

Animations

For CSS animations, a similar approach is used:

$("#someSelector").bind("animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd", function(){ ... });
Copy after login

Ensuring One-Time Firing

To prevent the event handler from running multiple times, use jQuery's .one() method. This ensures the handler fires only once, after which it is automatically removed:

$("#someSelector").one("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function(){ ... });

$("#someSelector").one("animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd", function(){ ... });
Copy after login

jQuery Method Deprecation

Note that the .bind() method has been deprecated in jQuery 1.7. Use .on() instead:

$("#someSelector")
  .on("animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd",
    function(e){
      // do something here
      $(this).off(e);
    }
  );
Copy after login

This equivalent code uses .off() to effectively mimic the behavior of .one().

References:

  • [jQuery Off Function](https://api.jquery.com/off/)
  • [jQuery One Function](https://api.jquery.com/one/)

The above is the detailed content of How Can jQuery Detect the Completion of CSS3 Transitions and Animations?. 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