How to make a CSS animation stop in the last state?

Asked

Viewed 1,969 times

4

I have a div, which is lively, and would not like that at the end of the animation she turns to the state of when she started, because she ends up returning to the original state at the end of the animation.

#div1 {
    height:100px;
    width:100px;
    background-color:silver;
    position:relative;
    animation-name:animacao;
    animation-duration:3s;
    animation-iteration-count:1;
    animation-delay:0.5s;
}
@keyframes animacao {
    from {
        left:0px
    }
    to {
        left:100px
    }
}
<div id="div1"></div>

jsfiddle: http://jsfiddle.net/fauqk0e5/

  • 1

    The fiddle does no animation, apart from presenting the square..

  • @Cesarmiguel Your browser must not support these properties without a prefix. Try http://jsfiddle.net/zb4rfgkk/ in Chrome.

  • @exact bfavaretto, missing prefixes

  • 1

    @Guilherme Beat me by 5s :)

  • @bfavaretto Seriously? It was cowboy luck, you’re still the fastest west kkkk

2 answers

6


There is an experimental property called animation-fill-mode which defines "how a CSS animation should apply the styles to the target before and after its execution". The value forwards determines that the element retains the styles of the last keyframe of animation.

Applying this, with the prefix -webkit for all properties, your CSS would look like this:

#div1 {
    height:100px;
    width:100px;
    background-color:silver;
    position:relative;
    -webkit-animation-name:animacao;
    -webkit-animation-duration:3s;
    -webkit-animation-iteration-count:1;
    -webkit-animation-delay:0.5s;
    -webkit-animation-fill-mode: forwards;
}
@-webkit-keyframes animacao {
    from {
        left:0px
    }
    to {
        left:100px
    }
}

http://jsfiddle.net/dp3ywmw9/

But note: the code with this prefix will only work in browsers that accept this prefix. It is recommended to include all prefixes in addition to an unfortified version (or use some JS to do this for you).

  • Nice to have commented that it retains the last keyframe of the animation, because if I do an animation with several keyframes, 10%,20%,.. it does not accumulate and simply stops at the end, it just retains the last keyframe and pauses the animation in it.

  • I think perhaps, the most ideal would be to run the animation with JS and in the end pause it in the last state

2

You can add the property -webkit-animation-fill-mode: forwards; not to return to initial position:

#div1 {
    height:100px;
    width:100px;
    background-color:silver;
    position:relative;
    -webkit-animation-name:animacao;
    -webkit-animation-duration:3s;
    -webkit-animation-iteration-count:1;
    -webkit-animation-delay:0.5s;
    -webkit-animation-fill-mode: forwards;

}
@-webkit-keyframes animacao {
    from {
        left:0px
    }
    to {
        left:100px
    }
}
  • We post the same thing almost at the same time!

  • I was running some tests on the browsers to see if it looked good on everyone, but you were 15 seconds faster, but with a very detailed answer :) +1

  • @bfavaretto But the prefixes of Cesar seem to be wrong, the right should not be so -webkit-* instead of webkit-*?

  • It’s true, @Guilhermenascimento

Browser other questions tagged

You are not signed in. Login or sign up in order to post.