Animation iteration count determines how many times an animation sequence will repeat.
Based on the provided reference, the animation-iteration-count
property in CSS is used to specify the number of times that an animation cycle is played before the animation stops. An animation cycle refers to one full play-through of the animation, from its beginning state to its end state (and potentially back to the beginning depending on the animation-direction
).
The initial value for animation-iteration-count
is '1'. This means that by default, an animation will play exactly once from its start to its end and then stop.
Understanding Animation Iteration Count
Think of an animation as a performance. The iteration count tells you how many times that performance will be shown.
- Value '1': The animation plays once.
- Value '2': The animation plays twice.
- Value '0.5': The animation plays only halfway through its sequence.
- Value 'infinite': The animation plays continuously without stopping.
Practical Examples
In web development using CSS, animation-iteration-count
is a property applied to an element that has an animation defined using @keyframes
.
Here's a simple CSS example:
.my-element {
animation-name: slidein; /* The name of the keyframes rule */
animation-duration: 3s; /* How long one cycle takes */
animation-iteration-count: 3; /* Play the animation 3 times */
}
@keyframes slidein {
from {
transform: translateX(0%);
}
to {
transform: translateX(100%);
}
}
In this example, the element with the class my-element
will slide to the right three times, with each slide taking 3 seconds.
Another common use case is for looping backgrounds or loading spinners:
.loading-spinner {
animation-name: spin;
animation-duration: 1s;
animation-iteration-count: infinite; /* Keep spinning forever */
animation-timing-function: linear;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
Here, the .loading-spinner
element will rotate continuously because its animation-iteration-count
is set to infinite
.
Key Values for Iteration Count
Value | Description |
---|---|
<number> |
Plays the animation that many times. |
infinite |
Plays the animation repeatedly without stopping. |
Using a number allows precise control over the number of repetitions, while infinite
is perfect for animations that should loop indefinitely.
Relation to animation-direction
As noted in the reference, animation-iteration-count
is often used alongside the animation-direction
property. animation-direction
controls whether the animation plays forwards, backwards, or alternates direction on each iteration. For example, if animation-direction
is set to alternate
, the first iteration plays forwards, the second backwards, the third forwards, and so on, up to the specified animation-iteration-count
.
Understanding animation-iteration-count
is fundamental to controlling the lifespan and repetition of animations in various digital contexts, especially in web design and graphical interfaces.