Maintaining the final state at end of a CSS3 animation

前端 未结 4 1680
天涯浪人
天涯浪人 2020-11-22 07:24

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

相关标签:
4条回答
  • 2020-11-22 07:33

    Use animation-fill-mode: forwards;

    animation-fill-mode: forwards;
    

    The element will retain the style values that is set by the last keyframe (depends on animation-direction and animation-iteration-count).

    Note: The @keyframes rule is not supported in Internet Explorer 9 and earlier versions.

    Working example

    div {
      width: 100px;
      height: 100px;
      background: red;
      position :relative;
      -webkit-animation: mymove 3ss forwards; /* Safari 4.0 - 8.0 */
      animation: bubble 3s forwards;
      /* animation-name: bubble; 
      animation-duration: 3s;
      animation-fill-mode: forwards; */
    }
    
    /* Safari */
    @-webkit-keyframes bubble  {
      0%   { transform:scale(0.5); opacity:0.0; left:0}
        50%  { transform:scale(1.2); opacity:0.5; left:100px}
        100% { transform:scale(1.0); opacity:1.0; left:200px}
    }
    
    /* Standard syntax */
    @keyframes bubble  {
       0%   { transform:scale(0.5); opacity:0.0; left:0}
        50%  { transform:scale(1.2); opacity:0.5; left:100px}
        100% { transform:scale(1.0); opacity:1.0; left:200px}
    }
    <h1>The keyframes </h1>
    <div></div>

    0 讨论(0)
  • 2020-11-22 07:45

    If you are using more animation attributes the shorthand is:

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

    This gives:

    • 2s duration
    • linear timing-function
    • 0.5s delay
    • 1 iteration-count (can be 'infinite')
    • normal direction
    • forwards fill-mode (set 'backwards' if you want to have compatibility to use the end position as the final state[this is to support browsers that has animations turned off]{and to answer only the title, and not your specific case})
    0 讨论(0)
  • 2020-11-22 07:51

    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;
    
    0 讨论(0)
  • 2020-11-22 07:54

    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;
    
    0 讨论(0)
提交回复
热议问题