Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Animate an object and retain its new position

Tags:

css

web

<html>
    <head>
        <title>Animation</title>

        <style type="text/css">
            .container
                {
                    background-color: blue;
                    height: 200px;
                    width: 200px;
                    position: relative;
                    -webkit-animation-name:animate;
                    -webkit-animation-duration:1s;  
                }

                @-webkit-keyframes animate  
                {
                    0%
                    {
                    -webkit-transform: translate(0, 0);
                    }

                    100% 
                    {
                    -webkit-transform: translate(100px, 100px);
                    }
                }
        </style>
    </head>

    <body>

    <div class="container">
    </div>

    </body>
</html>

The above code animates an object,but after the animation it returns to original position.How to retain it in the new position? which means the square must not return to (0,0)

like image 322
ThunderPunch Avatar asked Sep 28 '22 20:09

ThunderPunch


2 Answers

You can use the animation-fill-mode property.

The animation-fill-mode property specifies a style for the element when the animation is not playing (when it is finished, or when it has a delay).

By default, CSS animations do not affect the element until the first keyframe is "played", and then stops once the last keyframe has completed. The animation-fill-mode property can override this behavior.

-CSS3 animation-fill-mode Property

-webkit-animation-fill-mode: forwards make the movement permanent, once the animation has run its course.

.container {
  background-color: blue;
  height: 200px;
  width: 200px;
  position: relative;
  -webkit-animation-name: animate;
  -webkit-animation-duration: 1s;
  -webkit-animation-fill-mode: forwards;
}
@-webkit-keyframes animate {
  0% {
    -webkit-transform: translate(0, 0);
  }
  100% {
    -webkit-transform: translate(100px, 100px);
  }
}
<div class="container">
</div>
like image 111
Ashesh Avatar answered Sep 30 '22 10:09

Ashesh


Now it will stay at the last frame. I have added the -webkit-transform: translate(100px,100px); to the container class.

             .container
            {
                background-color: blue;
                height: 200px;
                width: 200px;
                position: relative;
                -webkit-animation-name:animate;
                -webkit-animation-duration:1s;  
              -webkit-animation-iteration-count: 1; 
                -webkit-transform: translate(100px,100px); 
            }

            @-webkit-keyframes animate  
            {
                0%
                {
                -webkit-transform: translate(0, 0);
                }

                100% 
                {
                -webkit-transform: translate(100px, 100px);
                }


            } 

http://jsbin.com/nimuniviqa/2/

like image 30
Prime Avatar answered Sep 30 '22 08:09

Prime