Home >Web Front-end >CSS Tutorial >How Can I Ensure CSS3 Animations Complete 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.
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!