Force CSS3 Animation Completion on :hover Element Exit
CSS3 animations in the :hover state provide an elegant way to add interactivity to elements. However, one limitation is that the animation aborts abruptly when the cursor leaves the element. Here's how to overcome this issue and force the animation to complete its execution, purely through CSS:
The provided animation, bounce, defines a series of keyframes that create a bouncing effect. To force the animation to continue even after the mouse exits the element, we adopt a clever technique.
-
Add a Class Trigger:
Create a separate class, say 'animated', with its corresponding animation (the same bounce keyframes you defined). -
Trigger the Animation with Hover:
In the :hover state of the element, instead of applying the animation directly, add the animated class. This class contains the actual animation. -
Remove the Trigger on Animation End:
Use JavaScript to listen for the animation end event. When the animation completes, remove the animated class from the element. This will effectively stop the animation.
Here's a modified example:
<style> @keyframes bounce { /* Same as before */ } .animated { animation: bounce 1s; } </style> <div class="box"> Hover me! </div>
$(".box").bind("webkitAnimationEnd mozAnimationEnd animationend", function(){ $(this).removeClass("animated"); }) $(".box").hover(function(){ $(this).addClass("animated"); })
With this approach, the animation will continue playing even after the cursor leaves the .box element. Visit this updated Fiddle for a live demonstration: http://jsfiddle.net/u7vXT/1.
The above is the detailed content of How Can I Ensure CSS3 Animations Complete on :hover Element Exit?. For more information, please follow other related articles on the PHP Chinese website!

Feedbin is the RSS reader I'm using at the moment. I was reading one of Harry's blog posts on it the other day, and I noticed a nice little interactive touch

This week, Chris Ferdinandi examined a clever JavaScript snippet, one that's written creatively with new syntax features, but is perhaps less readable and

Creating CSS-based animations and transitions can be a challenge. They can be complex and time-consuming. Need to move forward with a project with little time

In this week's news, Google defines guidelines for favicons, a new a11y testing tool from The Paciello Group, and changes to how the W3C plans to engage the community, plus more.

Here's a container with some child elements:

Flyout menus! The second you need to implement a menu that uses a hover event to display more menu items, you're in tricky territory. For one, they should

"The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect."- Tim Berners-Lee

In this week's roundup: datepickers are giving keyboard users headaches, a new web component compiler that helps fight FOUC, we finally get our hands on styling list item markers, and four steps to getting webmentions on your site.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft