Sub topic 8
Easing, duration and fill mode in scroll-driven animations
This chapter assumes you know @keyframes, the animation shorthand, and how animation-timeline and animation-range attach an animation to scroll. Here you will see how three familiar properties behave when scroll position replaces the clock.
Easing maps scroll to progress
A timeline reports progress from 0% to 100%. The easing function reshapes that progress before the keyframes use it. With linear, an element moves in direct proportion to the scrollbar. With ease, the browser's default, the motion speeds up and slows down relative to your finger, so a progress bar drifts away from the scroll position.
For anything that should track scrolling, such as a progress bar or a parallax layer, use linear. Use a non-linear curve for short reveals, where the motion should feel like a gesture instead of a gauge.
The default is easy to forget. If you write animation: grow both; with no timing function, you get ease.
@keyframes grow {
from { transform: scaleX(0); }
to { transform: scaleX(1); }
}
.progress {
position: fixed;
inset: 0 0 auto 0;
height: 4px;
background: rebeccapurple;
transform-origin: left;
animation: grow linear both;
animation-timeline: scroll(root block);
}You can also set easing inside the keyframes. A animation-timing-function on a keyframe applies to the interval that starts there, not to the whole animation.
@keyframes reveal {
from {
opacity: 0;
transform: translateY(40px);
animation-timing-function: ease-out;
}
to {
opacity: 1;
transform: none;
}
}Duration is not the speed
In a time-driven animation, animation-duration sets how long the animation runs. In a scroll-driven one, the timeline decides the length. The animation spans the whole timeline, or the part you select with animation-range. Speed therefore depends on how far the user scrolls, not on a number of seconds.
Leave the duration at its default of auto. That is why animation: grow linear both; works without a time value. A page that is twice as tall makes the same bar fill half as fast per pixel.
Fill mode covers the parts outside the range
A timeline is often longer than the range you use. A view timeline runs while the element crosses the scrollport, but entry 0% cover 30% uses only part of that. Outside the range the animation is not active, so animation-fill-mode decides what you see.
| Value | Before the range | After the range |
|---|---|---|
| `none` | Base styles | Base styles |
| `backwards` | First keyframe | Base styles |
| `forwards` | Base styles | Last keyframe |
| `both` | First keyframe | Last keyframe |
Consider the .card reveal. Its base style is fully visible. Without backwards, a card below the viewport shows at full opacity until it enters, then jumps to opacity: 0 and fades in. This is a visible flash. With both, the card starts hidden and ends in its final state.
.card {
animation: reveal linear both;
animation-timeline: view();
animation-range: entry 0% cover 30%;
}Declare animation-timeline after the shorthand. The shorthand resets it to auto.
Common mistakes
- Leaving out
linear, so a progress bar lags or overshoots the scroll position. - Setting a duration in seconds and expecting it to change the scroll speed.
- Omitting
both, so elements flash between their base and animated styles at the edges of the range. - Using
animation-iteration-count: infiniteoralternate. A timeline has one pass, so these rarely do what you want.
Exercise
A card uses animation: reveal ease-in-out forwards; animation-timeline: view(); animation-range: entry 0% cover 30%;. The keyframes go from opacity: 0 to opacity: 1, and the card's base style is opacity: 1. What does the user see while the card is still below the viewport, and which change fixes it?
Show answer
The card is fully visible, because forwards does not apply before the range and the base style is opacity: 1. It then snaps to opacity: 0 when the range starts and fades in. Changing forwards to both holds the first keyframe before the range, so the card stays hidden until it enters.