248

I have a 4 part CSS3 animation playing on click - but the last part of the animation is meant to take it off the screen.

However, it always goes back to its original state once it has played. Anyone know how I can stop it on its last css frame (100%), or else how to get rid of the whole div it is in once it has played.

@keyframes colorchange {
  0%   { transform: scale(1.0) rotate(0deg); }
  50%  { transform: rotate(340deg) translate(-300px,0px) }
  100% { transform: scale(0.5) rotate(5deg) translate(1140px,-137px); }
}
3

8 Answers 8

478

You're looking for:

animation-fill-mode: forwards;

More info on MDN and browser support list on canIuse.

6
  • 8
    Do you know if that works in chrome? Not having any luck with it
    – user531192
    Dec 6, 2010 at 7:58
  • 4
    Please accept this as the correct answer. More explanation on this one: 4waisenkinder.de/blog/2014/10/13/…
    – miron
    Oct 15, 2014 at 8:29
  • @user531192 For Chrome/Webkit browsers you have to use -webkit-animation-fill-mode: forwards;
    – eivindml
    Sep 22, 2015 at 10:36
  • 1
    Chrome doesn't need the -webkit- prefix anymore now (at least from v49)
    – Capsule
    Apr 13, 2016 at 1:52
  • 1
    One issue I had with this was I didn't have a 100% {} or to {} set, so even with animation-fill-mode: forwards; my animation would still dump me at 0%. (I was using some @for loops to create my animations and forgot to manually add a from{}and to{});
    – Phil Tune
    Sep 27, 2017 at 18:19
48

If you want to add this behaviour to a shorthand animation property definition, the order of sub-properties is as follows

animation-name - default none

animation-duration - default 0s

animation-timing-function - default ease

animation-delay - default 0s

animation-iteration-count - default 1

animation-direction - default normal

animation-fill-mode - you need to set this to forwards

animation-play-state - default running

Therefore in the most common case, the result will be something like this

animation: colorchange 1s ease 0s 1 normal forwards;

See the MDN documentation here

34
-webkit-animation-fill-mode: forwards; /* Safari 4.0 - 8.0 */
animation-fill-mode: forwards;

Browser Support

  • Chrome 43.0 (4.0 -webkit-)
  • IE 10.0
  • Mozilla 16.0 ( 5.0 -moz-)
  • Shafari 4.0 -webkit-
  • Opera 15.0 -webkit- (12.112.0 -o-)

Usage:-

.fadeIn {
  animation-name: fadeIn;
    -webkit-animation-name: fadeIn;

    animation-duration: 1.5s;
    -webkit-animation-duration: 1.5s;

    animation-timing-function: ease;
    -webkit-animation-timing-function: ease;

     animation-fill-mode: forwards;
    -webkit-animation-fill-mode: forwards;
}


@keyframes fadeIn {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

@-webkit-keyframes fadeIn {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}
6

The best way seems to put the final state at the main part of css. Like here, i put width to 220px, so that it finally becomes 220px. But starting to 0px;

div.menu-item1 {
  font-size: 20px;
  border: 2px solid #fff;
  width: 220px;
  animation: slide 1s;
  -webkit-animation: slide 1s; /* Safari and Chrome */
}
@-webkit-keyframes slide { /* Safari and Chrome */
  from {width:0px;}
  to {width:220px;}
}  
2
  • This is great, the best way to deal with this problem I think.
    – Drew Baker
    Apr 13, 2017 at 7:36
  • If you use an animation-delay, it starts with the end state, resets to 0%, then animates to 100%. Not ideal and visually jarring.
    – Deborah
    Jul 16, 2020 at 22:59
3

Isn't your issue that you're setting the webkitAnimationName back to nothing so that's resetting the CSS for your object back to it's default state. Won't it stay where it ended up if you just remove the setTimeout function that's resetting the state?

1

I just posted a similar answer, and you probably want to have a look at:

http://www.w3.org/TR/css3-animations/#animation-events-

You can find out aspects of an animation, such as start and stop, and then, once say the 'stop' event has fired you can do whatever you want to the dom. I tried this out some time ago, and it can work, but I'd guess you're going to be restricted to webkit for the time being (but you've probably accepted that already). Btw, since I've posted the same link for 2 answers, I'd offer this general advice: check out the W3C - they pretty much write the rules and describe the standards. Also, the webkit development pages are pretty key.

0

Nobody actualy brought it so, the way it was made to work is animation-play-state set to paused.

-1

I learned today that there is a limit you want to use for the fill-mode. This is from an Apple dev. Rumor is * around * six, but not certain. Alternatively, you can set the initial state of your class to how you want the animation to end, then * initialize * it at from / 0% .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.