Sub topic 6
View progress: animating elements as they enter and leave with view()
This chapter covers view progress timelines. You should already know CSS @keyframes, the animation properties, how scroll containers work, and how transform and opacity behave. You should also have met animation-timeline: scroll() in the previous chapters.
What a view progress timeline measures
A scroll progress timeline tracks how far a scroller has moved. A view progress timeline tracks something different: how far one element has moved through the visible area of its scroller. That visible area is called the scrollport.
The timeline is attached to the element that is animated. Each element gets its own timeline, so a list of 50 cards gives you 50 independent timelines, each with its own progress.
Progress is 0% when the element's box first touches the scrollport edge as it comes into view. It reaches 100% when the element has fully left on the other side. The element moves through several phases on the way.
The named phases are used later by animation-range. They are:
| Range name | Starts when | Ends when |
|---|---|---|
| `entry` | The element's leading edge reaches the scrollport | The element is fully inside the scrollport |
| `contain` | The element is fully inside (or fully covers) the scrollport | The element starts to leave |
| `exit` | The element starts to leave | The element has fully left |
| `cover` | The element's leading edge enters | The element's trailing edge leaves |
cover is the whole span. entry, contain and exit split that span into parts. For an element larger than the scrollport, contain describes the period when the element covers the whole scrollport.
Writing the basic rule
You attach a view progress timeline with the view() function:
@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%;
}Each part does one job:
@keyframes revealdescribes the start and end states. Nothing in it mentions scrolling.animation: reveal linear bothnames the keyframes, sets linear easing and applies the fill modeboth.animation-timeline: view()replaces the default time-based timeline with the element's own view progress.animation-range: entry 0% cover 30%limits the animation to part of the timeline. It starts when the card begins to enter and ends when the card is 30% of the way through thecoverrange.
Without the animation-range line, the animation would spread across the whole journey of the card. The card would still be partly transparent when it was already in the middle of the screen.
Declaration order matters
The animation shorthand resets animation-timeline to its initial value, auto. Declare animation-timeline after the shorthand, as in the example above. If you put it before the shorthand, the timeline is overwritten and the animation falls back to running over time.
Also, a scroll-driven animation ignores animation-duration for timing purposes. The browser maps the range onto progress, so a duration of 1s has no effect on speed. Some browsers need animation-duration: 1ms (or auto) to be valid in older implementations. The auto value is the default in current browsers, so you can usually leave duration out.
The arguments of view()
view() takes two optional arguments, in either order:
animation-timeline: view(<axis> <inset>);- The axis is
block(the default),inline,xory. It selects which scroll direction the timeline follows. In horizontal writing modes,blockis vertical. - The inset adjusts the edges of the scrollport used for the calculation. It takes one or two values, each a length, a percentage or
auto.
The inset is useful when you do not want the "visible" area to be the full viewport. A fixed header that covers the top 80px is one example:
.card {
animation: reveal linear both;
animation-timeline: view(block 80px 0px);
animation-range: entry 0% entry 100%;
}The first inset value moves the start edge, here the top, inward by 80px. The second moves the end edge. A positive value shrinks the area. A negative value grows it, which starts the animation before the element is visible.
The scroller for view() is the nearest ancestor that scrolls. You cannot choose a different one inside view(). If you need that, use a named timeline, covered in a later chapter.
Entry, exit and both together
A single set of keyframes can describe the whole journey. Use percentages and a range over cover:
@keyframes fade-through {
0% { opacity: 0; transform: scale(0.9); }
30% { opacity: 1; transform: scale(1); }
70% { opacity: 1; transform: scale(1); }
100% { opacity: 0; transform: scale(0.9); }
}
.panel {
animation: fade-through linear both;
animation-timeline: view();
}The default range is cover, so the element fades in, holds, and fades out. This is easy to write but couples entry and exit in one keyframes block.
A second approach uses two animations, each with its own range:
@keyframes fade-in {
from { opacity: 0; }
to { opacity: 1; }
}
@keyframes fade-out {
from { opacity: 1; }
to { opacity: 0; }
}
.panel {
animation: fade-in linear both, fade-out linear both;
animation-timeline: view(), view();
animation-range: entry 0% entry 100%, exit 0% exit 100%;
}Lists in these properties match up by position. The first animation uses the first timeline and range, and the second animation uses the second ones. If a list is shorter than the animation list, it repeats.
The second approach lets you change the entry and exit independently.
Why fill mode matters here
A range does not cover the whole timeline, so the animation is inactive for the rest of it. Without a fill mode, the element would snap back to its unanimated style outside the range.
With animation: reveal linear both:
backwardsapplies thefromkeyframe before the range starts. The card stays invisible while it is still below the viewport.forwardskeeps thetokeyframe after the range ends. The card stays visible after it has been revealed.
Leave both out and a card that is waiting to enter may flash at full opacity, then jump to transparent as the range begins.
Common errors
- The animation runs on a timer. The
animationshorthand came afteranimation-timeline, so the timeline was reset. Moveanimation-timelinebelow it. - Nothing animates. The nearest ancestor does not scroll, or the element is inside a container with
overflow: hiddenthat has no scrollable content.view()follows the nearest scroll container, even one that cannot actually scroll. - The reveal ends too late. The default range is
cover. Setanimation-rangetoentry 0% entry 100%or similar to finish earlier. - Elements at the top of the page start already revealed. An element already inside the scrollport at scroll position 0 begins partway through
entry. It cannot play the early part of its range. Use a range that starts later, or accept that those elements begin in a partly animated state. - The element jumps in place. Animating
top,heightormargincan change the element's own box, which changes its view progress and creates feedback. Animatetransformandopacity.
The last error deserves a closer look. The view timeline is computed from the element's layout position. If the animation changes that position, the progress changes again. Transforms do not alter the layout box, so they avoid the loop. The same reasoning is why transforms are also cheap to render, as the performance chapter explains.
Checking your work
Open the page in a browser that supports scroll-driven animations and the developer tools animation panel. In Chromium-based browsers, the panel shows the timeline and lets you scrub it. Check three things:
- The range markers sit where you expect.
- The element is in its start state before the range and its end state after.
- Scrolling backwards plays the animation in reverse.
The last point is a feature. A scroll-driven animation always matches the scroll position, so it is fully reversible.
Exercise
A card uses animation: reveal linear both; animation-timeline: view(); with no animation-range. It reaches full opacity only when it is leaving the top of the screen. Which declaration fixes this so the card is fully visible once it is entirely inside the scrollport, and why?
Show answer
Add animation-range: entry 0% entry 100%;. The default range is cover, which spans the card's whole trip from first touching the scrollport to fully leaving it, so the to keyframe is reached only at the end. The entry range ends when the card is fully inside the scrollport, so the animation finishes then. The both fill mode keeps the to state afterward.
Exercise
You write .item { animation-timeline: view(); animation: slide linear both; } and the animation plays over time instead of following scroll. What is wrong, and how do you fix it?
Show answer
The animation shorthand resets animation-timeline to auto, and it comes second, so it overwrites the earlier line. Put animation-timeline: view(); after the shorthand.