The animation
property is
a shorthand property for six of the animation properties:
The animation
property calls and controls an @keyframe
animation.
Like this:
.element-to-animate { animation: MyAnimation 5s infinite; } @keyframes MyAnimation { 0% { opacity: 0; } 100% { opacity: 1; } }
The following code shows how to use vendor prefixes to get better browser support.
#box { -webkit-animation: MyAnimationName 5s infinite; -moz-animation: MyAnimationName 5s infinite; -o-animation: MyAnimationName 5s infinite; animation: MyAnimationName 5s infinite; } @-webkit-keyframes MyAnimationName { 0% { opacity: 0; } 100% { opacity: 1; } } @-moz-keyframes MyAnimationName { 0% { opacity: 0; } 100% { opacity: 1; } } @-o-keyframes MyAnimationName { 0% { opacity: 0; } 100% { opacity: 1; } } @keyframes MyAnimationName { 0% { opacity: 0; } 100% { opacity: 1; } }
The following code shows how to add multiple steps in animation.
@keyframes MyAnimationName { 0% { font-size: 10px; } 30% { font-size: 15px; } 100% { font-size: 12px; } } #box { animation: MyAnimationName 2s infinite; }
The following code creates the keyframe animation with separate properties.
#box {
animation-name: MyAnimationName;
animation-duration: 4s;
animation-iteration-count: 10;
animation-direction: alternate;
animation-timing-function: ease-out;
animation-fill-mode: forwards;
animation-delay: 2s;
}
animation: name duration timing-function delay iteration-count direction fill-mode play-state;
In the following code,
animation: test 1s 2s 3 alternate backwards
1s
is duration, 2s
is delay, 3
is iterations.
animation |
4.0 -webkit- | 10.0 | 16.0 (5.0 -moz-) | 4.0 -webkit- | 15.0 -webkit-(12.0 -o-) |
<!DOCTYPE html>
<html>
<head>
<style>
div {<!--from w w w .j a v a 2 s .c o m-->
width: 100px;
height: 100px;
background: black;
position: relative;
-webkit-animation: mymove 5s infinite; /* Chrome, Safari, Opera */
animation: mymove 5s infinite;
}
/* Chrome, Safari, Opera */
@-webkit-keyframes mymove {
from {left: 0px;}
to {left: 200px;}
}
@keyframes mymove {
from {left: 0px;}
to {left: 200px;}
}
</style>
</head>
<body>
<div></div>
</body>
</html>