Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css3 animation on :hover; force entire animation

Tags:

css

animation

I've created a simple bounce animation which i'm applying to the :hover state of an element:

@keyframes bounce {     0% {       top: 0;       animation-timing-function: ease-out;     }     17% {       top: 15px;       animation-timing-function: ease-in;     }     34% {       top: 0;       animation-timing-function: ease-out;     }     51% {       top: 8px;       animation-timing-function: ease-in;     }     68% {       top: 0px;       animation-timing-function: ease-out;     }     85% {       top: 3px;       animation-timing-function: ease-in;     }     100% {       top: 0;     } }  .box:hover {      animation: bounce 1s; } 

The animation works fine, with the exception that when you remove your cursor from the element it abruptly stops. Is there anyway to force it to continue the set number of iterations even if the mouse has exited? Basically what I'm looking for here is an animation triggered by the :hover state. I'm not looking for a javascript solution. I haven't seen anyway to do this in the spec, but maybe there's an obvious solution I've missed here?

Here's a fiddle to play with: http://jsfiddle.net/dwick/vFtfF/

like image 864
David Wick Avatar asked Oct 08 '11 02:10

David Wick


People also ask

How do you animate smoothly in CSS?

The animation-timing-function specifies the speed curve of an animation. The speed curve defines the TIME an animation uses to change from one set of CSS styles to another. The speed curve is used to make the changes smoothly.

Does CSS animation affect performance?

Compared with animating elements using JavaScript, CSS animations can be easier to create. They can also give better performance, as they give the browser more control over when to render frames, and to drop frames if necessary.


1 Answers

I'm afraid that the only way to solve this is with a bit of javascript, you must add the animation as a class and then remove it when the animation finishes.

$(".box").bind("webkitAnimationEnd mozAnimationEnd animationend", function(){   $(this).removeClass("animated")   })  $(".box").hover(function(){   $(this).addClass("animated");         }) 

http://jsfiddle.net/u7vXT/

like image 180
methodofaction Avatar answered Oct 02 '22 13:10

methodofaction