Home > Web Front-end > CSS Tutorial > How Can I Dynamically Re-trigger WebKit CSS Animations?

How Can I Dynamically Re-trigger WebKit CSS Animations?

Susan Sarandon
Release: 2024-12-01 22:20:11
Original
352 people have browsed it

How Can I Dynamically Re-trigger WebKit CSS Animations?

Re-triggering WebKit CSS Animations Dynamically

When working with CSS animations powered by WebKit, there may be instances where you need to re-trigger an animation after its initial execution. To achieve this without relying on timeouts or multiple animations, we can leverage a built-in event: "webkitAnimationEnd."

This event fires when the animation completes, allowing us to reset and restart it programmatically. Here's how:

In Plain JavaScript:

var element = document.getElementById('box');

element.addEventListener('webkitAnimationEnd', function(){
    this.style.webkitAnimationName = '';
}, false);

document.getElementById('button').onclick = function(){
    element.style.webkitAnimationName = 'shake';
    // Prevent default browser behavior here if needed.
};
Copy after login

In jQuery:

var $element = $('#box').bind('webkitAnimationEnd', function(){
    this.style.webkitAnimationName = '';
});

$('#button').click(function(){
    $element.css('webkitAnimationName', 'shake');
    // Prevent default browser behavior here if needed.
});
Copy after login

This approach provides a clean and effective way to re-trigger CSS animations on demand. It eliminates the need for complex timeouts or additional animations, ensuring smooth and seamless transitions.

The above is the detailed content of How Can I Dynamically Re-trigger WebKit CSS 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