css - Maintaining the final state at end of a CSS3 animation


Translate

I'm running an animation on some elements that are set to opacity: 0; in the CSS. The animation class is applied onClick, and, using keyframes, it changes the opacity from 0 to 1 (among other things).

Unfortunately, when the animation is over, the elements go back to opacity: 0 (in both Firefox and Chrome). My natural thinking would be that animated elements maintain the final state, overriding their original properties. Is this not true? And if not, how can I get the element to do so?

The code (prefixed versions not included):

@keyframes bubble {
    0%   { transform:scale(0.5); opacity:0.0; }
    50%  { transform:scale(1.2); opacity:0.5; }
    100% { transform:scale(1.0); opacity:1.0; }
}

所有的回答
  • Translate

    Try adding animation-fill-mode: forwards;. For example like this:

    -webkit-animation: bubble 1.0s forwards; /* for less modern browsers */
            animation: bubble 1.0s forwards;
    

  • Translate

    If you are using more animation attributes the shorthand is:

    animation: bubble 2s linear 0.5s 1 normal forwards;
    

    for 2s duration, linear timing-function, 0.5s delay, 1 iteration-count, normal direction, forward fill-mode


  • Translate

    IF NOT USING THE SHORT HAND VERSION: Make sure the animation-fill-mode: forwards is AFTER the animation declaration or it will not work...

    animation-fill-mode: forwards;
    animation-name: appear;
    animation-duration: 1s;
    animation-delay: 1s;
    

    vs

    animation-name: appear;
    animation-duration: 1s;
    animation-fill-mode: forwards;
    animation-delay: 1s;