Home > Web Front-end > CSS Tutorial > Can CSS3 :hover Animations Be Forced to Complete Their Cycle After Mouseout?

Can CSS3 :hover Animations Be Forced to Complete Their Cycle After Mouseout?

Patricia Arquette
Release: 2024-12-03 20:29:13
Original
584 people have browsed it

Can CSS3 :hover Animations Be Forced to Complete Their Cycle After Mouseout?

CSS3 Animation on :hover; Forcing the Entire Animation

In CSS3, a common animation technique is using :hover to trigger an animation on an element. However, when the cursor is removed from the element, the animation abruptly stops.

Question

Can CSS3 animations on :hover be forced to continue the full animation cycle even after the mouse leaves the element?

Answer

Unfortunately, CSS3 alone cannot accomplish this. The only solution is to use a combination of CSS and JavaScript.

JavaScript Solution

Here's how to achieve the desired result using JavaScript:

  • Add the animation as a class to the element.
  • Remove the animation class once the animation finishes.
$(".box").bind("webkitAnimationEnd mozAnimationEnd animationend", function(){
  $(this).removeClass("animated")  
})

$(".box").hover(function(){
  $(this).addClass("animated");        
})
Copy after login

This approach ensures that the animation continues its full cycle regardless of the cursor's position on the element.

The above is the detailed content of Can CSS3 :hover Animations Be Forced to Complete Their Cycle After Mouseout?. 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