Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS3 Animate: How to have the object not revert to its initial position after animation has run? [duplicate]

Tags:

css

I am attempting a simple transform on a shape in CSS (webkit specifically). The animation runs as expected but upon completion the div will revert to its initial state. Is there any way to have it remain in its final state?

Heres my CSS thus far:

    @-webkit-keyframes rotate-good {
  from {
    -webkit-transform: rotate(0deg);
  }
  to { 
    -webkit-transform: rotate(-90deg);
  }
}

@-webkit-keyframes rotate-bad {
  from {
    -webkit-transform: rotate(0deg);
  }
  to { 
    -webkit-transform: rotate(90deg);
  }
}

#good-arrow 
{
    -webkit-animation-name:             rotate-good; 
    -webkit-animation-duration:         1s; 
    -webkit-animation-iteration-count:  1;
    -webkit-animation-timing-function: linear;
    -webkit-animation-delay: 2s;
}

#bad-arrow 
{
    -webkit-animation-name:             rotate-bad; 
    -webkit-animation-duration:         1s; 
    -webkit-animation-iteration-count:  1;
    -webkit-animation-timing-function: linear;    
    -webkit-animation-delay: 2s;
}
like image 442
Sergio Avatar asked Aug 01 '11 11:08

Sergio


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 I animate persist in CSS?

Use 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).


3 Answers

A briefer way to do this is to add:

-webkit-animation-fill-mode: forwards;

which retains the final keyframe state.

Update: full cross browser

-webkit-animation-fill-mode: forwards;
-moz-animation-fill-mode: forwards;
-ms-animation-fill-mode: forwards;
-o-animation-fill-mode: forwards;
animation-fill-mode: forwards;
like image 143
Michael Mullany Avatar answered Oct 19 '22 23:10

Michael Mullany


the cross-browser solution is:

-webkit-animation-fill-mode: forwards;
-moz-animation-fill-mode: forwards;
-ms-animation-fill-mode: forwards;
-o-animation-fill-mode: forwards;
animation-fill-mode: forwards;

What Madara Uchiha comments above is not always possible for one reason: Imagine instead of starting the animation right away (animation-delay:0s) you want 10 sec of delay before it starts. If so, you would see the final state of your animated element for 10 sec, and then the animation would take it to de 0 keyframe to 100 keyframe transition, but always you are seeing for 10 seconds the ending state.

like image 38
Jata Avatar answered Oct 20 '22 00:10

Jata


Oh, that's easy, simply set all the css rules to the finishing result. Example

like image 27
Madara's Ghost Avatar answered Oct 20 '22 01:10

Madara's Ghost