CSS3 Hanging animation

Problem

Is there a way to achieve a hanging animation of an element bouncing up and down using margin-top? Something that imitates elastic.

Basically i want the first frame to be margin-top:0px, the second frame: margin-top:15px; and should be infinite.

Problem courtesy of: Ryan

Solution

There are lots of example on the web.

Check it out from this tutorial. It is done with pure CSS.


I just created a simple demo. Check it out. This one runs on both webkit and mozilla browsers.

@-webkit-keyframes bounce {
       from{ -webkit-transform: translate(0px,0px); }
       to { -webkit-transform: translate(0px,-30px); }  
      }

@-moz-keyframes bounce {
       from { -moz-transform: translate(0px,0px); }
       to { -moz-transform: translate(0px,-30px); }  
      }

.bounce{
    display:block;
    height:20px;
    width:20px;
    background:#ff0000;
    border-radius:20px;
    margin:50px;
    -webkit-animation-name: bounce;
        -webkit-animation-duration:.3s;
        -webkit-animation-direction:alternate;
        -webkit-animation-timing-function:linear;
        -webkit-animation-delay:0s;
        -webkit-animation-iteration-count:infinite;
    -moz-animation-name: bounce;
        -moz-animation-duration:.3s;
        -moz-animation-direction:alternate;
        -moz-animation-timing-function:linear;
        -moz-animation-delay:0s;
        -moz-animation-iteration-count:infinite;
}

Check out this demo

Hope you like it.

Solution courtesy of: Starx

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.