Sub topic 8
Easing, duration and fill mode in scroll-driven animations
This chapter assumes you know @keyframes, the animation-* properties, and the basics of animation-timeline with scroll() and view(). Here you will see how three familiar properties behave when scroll position replaces the clock: animation-timing-function, animation-duration, and animation-fill-mode. They still exist and still work. The mistakes come from reading them as time settings.
What changes when scroll drives the animation
A time-driven animation advances because time passes. A scroll-driven animation advances because the scroll position changes. The browser turns the timeline into a progress value from 0% to 100%. Then it runs the same pipeline as any CSS animation:
- Convert timeline progress into animation progress, using the range.
- Apply the easing function to that progress.
- Interpolate between keyframes.
- Outside the active range, apply the fill mode rules.
Easing and fill mode act on the progress value, and nothing in the pipeline is a clock. Duration is the odd one out, because there is no time to measure.
Easing
animation-timing-function remaps animation progress. With a time-driven animation this changes how the motion feels. With a scroll-driven one it changes how closely the animated property tracks your scroll position.
With linear, a scroll position at 50% gives an animation progress of 50%. With the default ease, 50% scroll gives roughly 80% progress, so the animation runs ahead of the scroll and then slows down near the end.
Many authors write animation: grow both; and forget the easing. The default timing function is ease, so the progress bar stops matching the page. This is the most common bug in a reading progress bar.
@keyframes grow {
from { transform: scaleX(0); }
to { transform: scaleX(1); }
}
/* Wrong: the bar runs ahead of the scroll position */
.progress-eased {
position: fixed;
inset: 0 0 auto 0;
height: 4px;
background: rebeccapurple;
transform-origin: left;
animation: grow ease both;
animation-timeline: scroll(root block);
}
/* Right: the bar width equals the scrolled fraction */
.progress {
position: fixed;
inset: 0 0 auto 0;
height: 4px;
background: rebeccapurple;
transform-origin: left;
animation: grow linear both;
animation-timeline: scroll(root block);
}Use linear whenever the animated value should represent the scroll position directly: progress bars, parallax offsets, and scrubbing a video-like sequence.
When non-linear easing is appropriate
Easing still helps for reveal effects, where the element should settle gently as it enters. Use a mild ease-out such as ease-out or cubic-bezier(0.2, 0.6, 0.3, 1). Keep three limits in mind:
- Easing is stateless. It maps position to progress and does not smooth or delay anything. Scrolling stops, and the animation stops at the same instant. Scroll back and it retraces its path.
- Overshooting curves, such as a bounce or a
cubic-bezierwith a value above 1, mean the element moves past its end state and returns while the user is still scrolling in one direction. This usually looks like a glitch. - Easing changes how the animation tracks scroll. It does not make it more efficient.
Per-keyframe easing
An animation-timing-function set inside a keyframe applies to the interval that starts at that keyframe. The function set on the element applies to each interval unless a keyframe overrides it. This lets one animation have a gentle start and a linear finish.
@keyframes reveal {
from {
opacity: 0;
transform: translateY(40px);
animation-timing-function: ease-out;
}
to {
opacity: 1;
transform: none;
}
}
.card {
animation: reveal linear both;
animation-timeline: view();
animation-range: entry 0% cover 30%;
}Here the from keyframe overrides the element's linear for the one interval it starts. With only two keyframes, that is the whole animation, so this card uses ease-out and the element-level linear has no effect. Per-keyframe easing matters when you have three or more keyframes.
Duration
A time-driven animation needs a duration to know how long to run. A scroll-driven animation does not. Its length comes from the timeline and animation-range. The default, animation-duration: auto, tells the browser to stretch the animation across the whole active range.
What happens if you set a time value?
- The animation does not take that many seconds. Scrolling decides how fast it advances.
- The value acts only as a unit for other time-based settings.
animation-delayandanimation-iteration-countare measured against it, so they behave in proportion to the duration. - The result is hard to predict and hard to read in code.
The practical rule is to leave animation-duration at auto and do not put a time in the shorthand. Use animation-range for timing instead. To start later or finish earlier, change the range. Do not add a delay.
The shorthand reset
The animation shorthand resets animation-timeline to auto, and in current specifications it also resets animation-range. Declare the shorthand first and the timeline and range after it. This is the same ordering rule that the progress bar example uses.
.card {
animation: reveal linear both; /* resets timeline and range */
animation-timeline: view(); /* must come after */
animation-range: entry 0% cover 30%;
}If you reverse the order, the shorthand overrides your timeline. The element then animates against the document timeline, which means over time. With a duration of auto that is zero seconds, so you see the final state at once and no animation.
Fill mode
A fill mode controls what an element looks like when the animation is not active. For a scroll-driven animation, "not active" means the scroll position is before or after the animation's range.
| Value | Before the range | After the range |
|---|---|---|
| `none` (default) | Element's normal styles | Element's normal styles |
| `backwards` | First keyframe applied | Element's normal styles |
| `forwards` | Element's normal styles | Last keyframe applied |
| `both` | First keyframe applied | Last keyframe applied |
For a full-timeline animation, such as the root progress bar, the animation is always active. Fill mode barely matters, though both is still a safe default. For a view() animation with a narrower animation-range, it matters a lot.
Pop-in before the range starts
Suppose a card uses this range:
@keyframes reveal {
from { opacity: 0; transform: translateY(40px); }
to { opacity: 1; transform: none; }
}
.card {
animation: reveal linear forwards;
animation-timeline: view();
animation-range: entry 25% cover 50%;
}The card becomes visible at entry 0%, but the animation does not begin until entry 25%. Without backwards, the card is fully opaque for the first quarter of its entry. At entry 25% it jumps to opacity: 0 and fades back in. Using both applies the from keyframe before the range, and the jump disappears.
Exit animations need forwards
An exit animation has the opposite problem. Its start state equals the element's normal styles, and its end state is different.
@keyframes fade-out {
to { opacity: 0; }
}
.banner {
animation: fade-out linear forwards;
animation-timeline: view();
animation-range: exit 0% exit 100%;
}Before the exit range, the banner shows its normal styles, which is what you want. After the range, forwards keeps it at opacity: 0. With the default none, the banner would snap back to full opacity the moment it finished exiting. In a scroller where the element is still partly visible, that flash is noticeable.
Choosing a fill mode
- Use
bothas the default unless you have a reason not to. - Use
forwardsalone when the start state must be the element's real styling, for example so the content stays readable if the animation never runs. - Use
backwardsalone when the end state equals the normal styles and only the start state needs forcing.
Direction and iteration
Two more properties take part in the same pipeline:
animation-direction: reversemaps the start of the range to the last keyframe and the end of the range to the first keyframe. This is useful when you have one set of keyframes and want to use it for both an entry and an exit.animation-iteration-count: 2plays the keyframes twice across the range, not twice over time. Scrolling through the range once cycles the animation two times. This suits decorative loops tied to scroll, such as a rotating icon.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Bar fills before you reach the bottom | Default `ease` timing | Set `linear` |
| No animation, element just shows end state | Shorthand declared after `animation-timeline` | Move the shorthand first |
| Element flashes visible then disappears at range start | Fill mode lacks `backwards` | Use `both` |
| Element snaps back after finishing its exit | Fill mode lacks `forwards` | Use `forwards` or `both` |
| Animation feels abrupt or hard to control | Time duration or delay set | Use `auto` and adjust `animation-range` |
Exercise
A reading progress bar uses animation: grow both; animation-timeline: scroll(root block);. When you scroll halfway down the page, the bar is already about 80% full. What is wrong, and what is the fix?
Show answer
The shorthand leaves animation-timing-function at its default, ease. Easing remaps scroll progress, so 50% scroll gives roughly 80% animation progress. Add linear: animation: grow linear both;.
Exercise
A card uses animation: reveal linear forwards; animation-timeline: view(); animation-range: entry 30% cover 40%;. The from keyframe sets opacity: 0. Describe what the user sees as the card scrolls into view, and fix it with one change.
Show answer
The card is fully visible from entry 0% to entry 30%, because the animation has not started and forwards does not apply the first keyframe. At entry 30% it jumps to opacity: 0 and then fades in. Change the fill mode to both, or add backwards, so the from keyframe applies before the range begins.