CSS3 Keyframe Animations: End and stay on the last frame

后端 未结 3 1555
鱼传尺愫
鱼传尺愫 2020-12-07 20:30

I\'ve run into some difficulty trying to play a CSS3 keyframe animation and have the relevant element stick at the last frame after the animation has completed. To my unders

相关标签:
3条回答
  • animation-fill-mode:forwards is the correct property to use. Is does not seem to work because the sprite image background has a default background-repeat:repeat, so the last frame you think you are seeing is actually the first frame of the repeated background image.

    If you set

    background: url("http://files.simurai.com/misc/sprite.png") no-repeat
    
    animation: play .8s steps(10) forwards;
    
    @keyframes play {
        from { background-position:    0px; }
        to { background-position: -500px; }
    }
    

    and run the demo the final frame is now blank - so forwards is doing what it should do. The second part of the solution is to change the final to and steps CSS properties to position the background correctly. So we really need the background to stop at -450px and use 9 steps.

    -webkit-animation: play .8s steps(9) forwards;
    
    @keyframes play {
        from { background-position: 0; }
        to { background-position: -450px; }
    }
    

    See demo - I only fixed the Chrome properties. Also here is the sample image in case the original disappears.

    .hi {
        width: 50px;
        height: 72px;
        background: url("http://i.stack.imgur.com/ilKfd.png") no-repeat;
        
        -webkit-animation: play .8s steps(9) forwards;
           -moz-animation: play .8s steps(10) infinite;
            -ms-animation: play .8s steps(10) infinite;
             -o-animation: play .8s steps(10) infinite;
                animation: play .8s steps(9) forwards;
    }
    
    @-webkit-keyframes play {
       from { background-position:    0px; }
         to { background-position: -450px; }
    }
    
    @-moz-keyframes play {
       from { background-position:    0px; }
         to { background-position: -500px; }
    }
    
    @-ms-keyframes play {
       from { background-position:    0px; }
         to { background-position: -500px; }
    }
    
    @-o-keyframes play {
       from { background-position:    0px; }
         to { background-position: -500px; }
    }
    
    @keyframes play {
       from { background-position:    0px; }
         to { background-position: -450px; }
    }
    <div class="hi"></div>

    0 讨论(0)
  • 2020-12-07 21:23

    The following code will make the transition stay on the last frame:

    -webkit-timing-function:ease;
    -webkit-iteration-count:1;
    
    0 讨论(0)
  • 2020-12-07 21:24

    Change 'infinite' to '1' in the css, this fixes it for me

    0 讨论(0)
提交回复
热议问题