animation-direction
Definition
animation-direction
is a CSS property that specifies the direction of an animation.
Examples
/* Normal playback */
#normal {
animation-name: example;
animation-duration: 4s;
animation-direction: normal;
animation-iteration-count: 1;
}
/* Reverse playback */
#reverse {
animation-name: example;
animation-duration: 4s;
animation-direction: reverse;
animation-iteration-count: 1;
}
/* Alternate playback */
#alternate {
animation-name: example;
animation-duration: 4s;
animation-direction: alternate;
animation-iteration-count: 2;
}
/* Alternate-reverse playback */
#alternate-reverse {
animation-name: example;
animation-duration: 4s;
animation-direction: alternate-reverse;
animation-iteration-count: 2;
}
In these examples, the animation named "example" will play once in the normal direction, once in the reverse direction, twice alternating between forward and backward playback, and twice alternating between backward and forward playback, respectively.
Values
Value | Description |
---|---|
normal | The animation plays forward from start to end. |
reverse | The animation plays backward from end to start. |
alternate | The animation plays forward, then backward, and then forward again, and so on. |
alternate-reverse | The animation plays backward, then forward, and then backward again, and so on. |
Best Practices
- Define the animation using the @keyframes rule before using it on an element.
- Use the animation-name property to specify the name of the animation, and the animation-duration property to specify the length of the animation.
- Use the animation-iteration-count property to specify the number of times the animation should play.
- Use the animation-delay property to specify a delay before the animation starts.
- Use the animation-fill-mode property to specify how the element should look when the animation is not playing.
- Use the animation-timing-function property to specify the speed curve of the animation.
- Use the animation-play-state property to pause and resume the animation.
- Avoid overusing animations, as they can be distracting and make the user experience feel cluttered.
Browser Compatibility
Chrome | Firefox | Safari | Internet Explorer | Microsoft Edge | Opera |
---|---|---|---|---|---|
Yes | Yes | Yes | Yes | Yes | Yes |