March 13, 2012
CSS3 Hanging animation
Question by Ryan
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.
Answer by Starx
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.