Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping a CSS animation but letting its current iteration finish

Tags:

css

animation

I have the following HTML:

<div class="rotate"></div>​ 

And the following CSS:

@-webkit-keyframes rotate {   to {      -webkit-transform: rotate(360deg);   } } .rotate {     width: 100px;     height: 100px;     background: red;     -webkit-animation-name:             rotate;     -webkit-animation-duration:         5s;     -webkit-animation-iteration-count:  infinite;     -webkit-animation-timing-function:  linear; }​ 

I want to know if there is a way (using JavaScript) to stop the animation but let it finish its current iteration (preferably by changing one or a few CSS properties). I have tried setting -webkit-animation-name to have a blank value but that causes the element to jump back to its original position in a jarring fashion. I have also tried setting -webkit-animation-iteration-count to 1 but that does the same thing.

like image 794
knpwrs Avatar asked Oct 25 '12 03:10

knpwrs


People also ask

Can you pause a CSS animation?

The only way to truly pause an animation in CSS is to use the animation-play-state property with a paused value. In JavaScript, the property is “camelCased” as animationPlayState and set like this: element. style.

How do you wait for an animation to finish CSS?

Use a transition time of 0.6s when you hover and an animation time of 0.01 when you hover off. This way, the animation will reset itself to its original position pretty much immediately and stop the funky behaviour.

Which CSS property sets if an animation is running or paused?

The animation-play-state CSS property sets whether an animation is running or paused.

What is animation-fill-mode in CSS?

The animation-fill-mode property specifies a style for the element when the animation is not playing (before it starts, after it ends, or both). CSS animations do not affect the element before the first keyframe is played or after the last keyframe is played. The animation-fill-mode property can override this behavior.


1 Answers

Stop the animation upon receiving an animationiteration event. Something like this (using jQuery):

CSS

@-webkit-keyframes rotate {   to {     -webkit-transform: rotate(360deg);   } } .rotate {     width: 100px;     height: 100px;     background: red; } .rotate.anim {     -webkit-animation-name:             rotate;     -webkit-animation-duration:         5s;     -webkit-animation-iteration-count:  infinite;     -webkit-animation-timing-function:  linear; } 

HTML

<div class="rotate anim">TEST</div> <input id="stop" type="submit" value="stop it" /> 

JS (JQuery)

$("#stop").click(function() {     $(".rotate").one('animationiteration webkitAnimationIteration', function() {         $(this).removeClass("anim");     }); }); 

Fiddle: http://jsfiddle.net/sSYYE/1/

Note that I'm using .one here (not .on) so that the handler only runs once. That way, the animation can later be restarted.

like image 161
nneonneo Avatar answered Oct 11 '22 08:10

nneonneo