Stopping a CSS animation but letting its current iteration finish

后端 未结 2 1288
一向
一向 2020-12-05 07:06

I have the following HTML:

And the following CSS:

@-webkit-keyframes rotate {
  t         


        
相关标签:
2条回答
  • 2020-12-05 07:30

    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.

    0 讨论(0)
  • 2020-12-05 07:49

    Do you mean you want the animation to stop where it is when it ends without jumping back to its original position?

    Then you want:

    Animate something moving from one place to another and have it stay there: animation-fill-mode:forwards;

    check this link:

    http://www.w3schools.com/cssref/css3_pr_animation-fill-mode.asp

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