Sub topic 7
Limiting the active part of the timeline with animation-range
This chapter assumes you know @keyframes, the animation shorthand, and how animation-timeline: view() attaches a view progress timeline to an element. By default, an animation stretches across the whole timeline. animation-range lets you pick a smaller part of it.
What the range does
A timeline has a start (0%) and an end (100%). Without a range, your keyframes run from the first point to the last. With animation-range, the from keyframe maps to the range start and the to keyframe maps to the range end. Outside the range, the animation sits at its first or last state, depending on animation-fill-mode.
The syntax takes a start and an end:
animation-range: <start> <end>;Each value is a percentage, a length, or a named range with an optional offset.
Named ranges for view timelines
A view progress timeline follows an element as it crosses the scrollport (the visible area of the scroller). The spec names the stages of that crossing:
| Name | Meaning |
|---|---|
| `entry` | The element is crossing the scrollport edge on the way in. |
| `exit` | The element is crossing the scrollport edge on the way out. |
| `contain` | The element is fully inside, or covers the scrollport if it is larger. |
| `cover` | The whole span, from first touching the scrollport to fully leaving it. |
| `entry-crossing` | The element's leading edge crosses the scrollport's end edge, but its trailing edge has not yet. |
| `exit-crossing` | The element's leading edge crosses the scrollport's start edge, but its trailing edge has not yet. |
Each name can be followed by a percentage, such as entry 25% or cover 30%. The percentage is a position inside that named range.
@keyframes reveal {
from { opacity: 0; transform: translateY(40px); }
to { opacity: 1; transform: none; }
}
.card {
animation: reveal linear both;
animation-timeline: view();
animation-range: entry 0% cover 30%;
}Here the start is the beginning of entry, when the card's top edge first touches the bottom of the scrollport. The end is 30% of the way through cover. The card is fully visible by then, and it stays visible because of both.
Reading a range
animation-range: entry 0% entry 100%;This runs the animation only during entry. The card finishes animating just as it becomes fully visible. This pattern is a common default for reveal effects.
animation-range: exit 0% exit 100%;This runs the animation only as the element leaves, which suits a fade-out.
If you give one named range alone, such as animation-range: entry;, the browser uses its start and end. This is the same as entry 0% entry 100%.
Ranges on scroll timelines
Named ranges belong to view timelines. A scroll progress timeline has no entry or cover, so use percentages or lengths:
.progress {
animation: grow linear both;
animation-timeline: scroll(root block);
animation-range: 10% 90%;
}The bar stays empty for the first 10% of the scroll and is full from 90% onward.
Longhands and the shorthand
animation-range is a shorthand for animation-range-start and animation-range-end. Use the longhands to change one side:
.card {
animation-range-start: entry 20%;
animation-range-end: contain 50%;
}Like animation-timeline, declare animation-range after the animation shorthand. The shorthand resets animation-range and animation-timeline to their initial values.
Common mistakes
- Forgetting fill mode. Without
bothorforwards, the element snaps back to its un-animated style once the range ends. - Using named ranges on a scroll timeline. They have no effect there. Use percentages.
- Expecting a duration to matter. Progress comes from scroll position, so
animation-durationdoes not control speed. The range does. - Starting too late. A range that begins at
cover 50%leaves the element in itsfromstate while it is already half visible.
Exercise
A card should fade in only while it is crossing the bottom edge of the scrollport, and it must stay fully visible afterwards. Which animation-timeline, animation-range and fill mode would you use?
Show answer
Use animation-timeline: view(); animation-range: entry 0% entry 100%; and a fill mode of both (or forwards), as in animation: reveal linear both. The range limits the animation to the entry phase, and the fill mode keeps the final keyframe applied after the range ends.