Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't stop css animation disappearing after last key frame

I've got a simple css animation that I'd like to play and then stop on the last frame fully displaying the image. But currently at the moment it plays then seems to revert back to frame one so the santas face disappears.

How can i make it play through once then stop on the last key frame or display the image without it fading out again?

http://jsfiddle.net/uy25Y/

    <img class="santaface" src="http://imgs.tuts.dragoart.com/how-to-draw-a-santa-face_1_000000001282_3.jpg">

     .santaface{
          opacity:0;
          position: absolute;
          left:40%; top:20%; width:20%;
            -webkit-animation-name: santaappear;
            -webkit-animation-duration: 13s;
            }


        .santaface{-webkit-animation-delay:2s;animation-delay:2s;}

        @-webkit-keyframes santaappear {
            0% { opacity:0;}
               96% {opacity:1;}
        }
like image 649
Dano007 Avatar asked Nov 28 '13 20:11

Dano007


People also ask

What CSS property ensures the animation stays in the end position after the animation finishes?

The animation-fill-mode property specifies a style for the element when the animation is not playing (before it starts, after it ends, or both).

How do you freeze animation in CSS?

The only way to truly pause an animation in CSS is to use the animation-play-state property with a paused value. In JavaScript, the property is “camelCased” as animationPlayState and set like this: element.

How do I make animations run forever in CSS?

To create infinite animations in CSS, we will use the property animation-iteration-count: infinite;.

How do you stop a transition in CSS?

To trigger an element's transition, toggle a class name on that element that triggers it. To pause an element's transition, use getComputedStyle and getPropertyValue at the point in the transition you want to pause it. Then set those CSS properties of that element equal to those values you just got.


1 Answers

You need animation-fill-mode: forwards to prevent this from happening.

Additionally, you need to end with an opacity of 1, therefore the last frame must have an opacity of 1.

jsFiddle example - it works as expected now.

You can also shorten your keyframe by removing 0%, as this is already given in the initial state.

@keyframes santaappear {
    96% {
        opacity:1;
    }
    100% {
        opacity:1;
    }
}

You could also combine 96% and 100%.

@keyframes santaappear {
    96%, 100% {
        opacity:1;
    }
}

Since you are using multiple animation properties, use the animation shorthand:

<single-animation-name> || <time> || <timing-function> || <time> || <single-animation-iteration-count> || <single-animation-direction> || <single-animation-fill-mode>`

Which would be:

animation: santaappear 13s 2s forwards;
-moz-animation: santaappear 13s 2s forwards;
-webkit-animation: santaappear 13s 2s forwards;

In the demo, I added vendor prefixes for -moz/-webkit. In addition to these you should have one written without a prefix. Same goes for the keyframes.

like image 99
Josh Crozier Avatar answered Oct 24 '22 20:10

Josh Crozier