Home > Web Front-end > JS Tutorial > How to Detect the End of CSS3 Transitions and Animations with jQuery?

How to Detect the End of CSS3 Transitions and Animations with jQuery?

Patricia Arquette
Release: 2024-11-04 06:46:30
Original
286 people have browsed it

How to Detect the End of CSS3 Transitions and Animations with jQuery?

Harnessing CSS3 Transitions: Detecting Completion with jQuery

When animating elements using CSS3 transitions, it becomes necessary to know the precise moment they conclude to perform subsequent actions. Unlike jQuery animations, CSS3 transitions don't have built-in completion callbacks.

Capturing Transition End Events

To detect the end of a transition, jQuery offers the following script:

<code class="javascript">$("#someSelector").bind("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function(){ ... });</code>
Copy after login

For animations, a similar approach can be used:

<code class="javascript">$("#someSelector").bind("animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd", function(){ ... });</code>
Copy after login

By passing all prefixed event strings, this script will work across multiple browsers.

Using .one() for Single Fire

To ensure the event handler fires only once, you can use jQuery's .one() method instead of .bind():

<code class="javascript">$("#someSelector").one("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function(){ ... });

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

Deprecation and Modern Approach

jQuery's .bind() method is now deprecated. The preferred approach is .on(), which works in conjunction with .off(). Here's an example:

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

This approach ensures that the callback function executes only once.

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