Sub topic 6
View progress: animating elements as they enter and leave with view()
This chapter assumes you know @keyframes, the animation properties, overflow and scroll containers, and transform and opacity. It also assumes you have seen animation-timeline: scroll(), which tracks how far a scroller has scrolled.
What a view progress timeline measures
A scroll progress timeline follows a scroller. A view progress timeline follows one element as it moves through a scrollport, which is the visible area of a scroll container. Progress is 0% when the element is about to enter the scrollport. It is 100% when the element has fully left.
You create one with animation-timeline: view(). The timeline belongs to the element you animate, and its subject is that same element.
@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%;
}Declare animation-timeline after the animation shorthand. The shorthand resets animation-timeline to its initial value, so a declaration placed before it is lost.
How the timeline progresses
The browser names several ranges within this timeline:
| Range name | Starts when | Ends when |
|---|---|---|
| `cover` | The element's leading edge touches the scrollport | The element's trailing edge leaves it |
| `entry` | The element begins to enter | The element is fully inside |
| `exit` | The element begins to leave | The element is fully gone |
| `contain` | The element is fully inside (or fills the scrollport, if larger) | It stops being fully inside |
Ranges are covered in detail in a later chapter. Here, the key point is that animation-range: entry 0% cover 30% tells the browser to run the animation from the start of entry to 30% of the way through the whole cover range. Outside that window, both for fill mode holds the first or last keyframe.
Arguments to view()
view() takes an optional axis and an optional inset:
.card {
animation-timeline: view(block);
}
.banner {
animation-timeline: view(inline 50px);
}- The axis is
block(the default),inline,x, ory. - The inset shrinks or grows the area used to judge visibility. A positive value moves the edges inward, so the element must travel further before it counts as visible.
- The scroller is always the nearest ancestor that scrolls. You cannot pick a different scroller with
view(). To do that, use a named timeline, covered later.
Animating on exit as well
You can use one keyframes rule for both ends by listing percentages. With no animation-range, the default is the full cover range, so 0% and 100% map to the element entering and leaving.
@keyframes fade-edges {
0% { opacity: 0; }
30% { opacity: 1; }
70% { opacity: 1; }
100% { opacity: 0; }
}
.item {
animation: fade-edges linear both;
animation-timeline: view();
}The item fades in during the first 30% of cover, stays visible, and fades out during the last 30%.
Common errors
- Animation never runs. Check that
animation-durationis not0sin a browser that needs it, and that the nearest scrolling ancestor really scrolls. If an ancestor hasoverflow: hidden, it becomes the scroller but never moves, so progress stays fixed. - Animation is stuck at the start. The
animation-timelinedeclaration came before theanimationshorthand. - Jumpy start state. Without
both(or at leastbackwards), the element shows its normal style until the range begins, then snaps to the first keyframe. - Easing feels wrong. The default
easecurve applies to scroll progress too. Uselinearso motion tracks the scrollbar.
Exercise
A card uses animation: reveal linear both; animation-timeline: view(); animation-range: exit 0% exit 100%;. When does the animation progress, and what does the card look like before that?
Show answer
The animation progresses only while the card is leaving the scrollport, from when it begins to exit to when it is fully gone. Before that, the fill mode both applies the first keyframe backwards, so the card shows the from state of reveal (for example, transparent and shifted) for the whole time it is entering and fully visible. To keep it visible until exit, the keyframes must start in the visible state.