Sub topic 1
Recap: Keyframes and the Animation Properties
Scroll-driven animations reuse the CSS animation system you already know. The keyframes stay the same. Only the thing that moves the animation forward changes. This chapter reviews the parts of that system that matter for the rest of the tutorial, so the later chapters can focus on what is new.
Prerequisites
This chapter assumes you already know:
- CSS selectors and how the cascade decides which declaration wins.
- The basics of
@keyframesand theanimationproperties. This chapter refreshes them, but it is not a first introduction. - What an overflow (scroll) container is, meaning an element with
overflow: autooroverflow: scrollwhose content can be taller or wider than the box. transformandopacity, the two properties most scroll-driven examples animate.
If any of these are unfamiliar, review them first. The later chapters build directly on them.
What @keyframes defines
An @keyframes rule is a named description of how property values change over the course of an animation. It does not say how long the animation lasts, when it starts, or what drives it. It only says what the element looks like at each point of progress.
@keyframes slide-in {
from {
opacity: 0;
transform: translateX(-24px);
}
to {
opacity: 1;
transform: translateX(0);
}
}Each point is a keyframe. from is the same as 0% and to is the same as 100%. You can add percentages in between:
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.15); }
100% { transform: scale(1); }
}Some details are worth remembering:
- If you omit
fromorto, the browser uses the element's current computed value for that property. This is useful when you want to animate "from wherever it is now". - Keyframes can be listed in any order in the source. The browser sorts them by percentage.
- Properties that cannot be interpolated, such as
display, do not animate smoothly. Most scroll-driven work sticks totransformandopacity. - A keyframe percentage means progress through the animation, not seconds. This matters later, because "progress" will map to scroll position instead of time.
Attaching keyframes with animation properties
A keyframes rule does nothing until an element uses it. The animation-* properties connect an element to a keyframes rule and describe how to play it.
| Property | Purpose | Initial value |
|---|---|---|
| `animation-name` | Which `@keyframes` rule to use | `none` |
| `animation-duration` | How long one iteration takes | `0s` |
| `animation-timing-function` | Easing between keyframes | `ease` |
| `animation-delay` | Wait before starting | `0s` |
| `animation-iteration-count` | How many times to repeat | `1` |
| `animation-direction` | Forward, reverse, or alternating | `normal` |
| `animation-fill-mode` | Styles applied before and after the active period | `none` |
| `animation-play-state` | Running or paused | `running` |
| `animation-timeline` | What drives progress | `auto` |
The last row is the one this tutorial is about. The other rows were available long before scroll-driven animations existed.
A complete time-driven example:
.badge {
animation-name: pulse;
animation-duration: 1.2s;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
}Because the initial duration is 0s, an animation with a name but no duration finishes immediately and appears to do nothing. This is the most common reason a first animation seems broken.
The animation shorthand
The animation shorthand sets most of these properties in one declaration:
.badge {
animation: pulse 1.2s ease-in-out infinite;
}The values can mostly appear in any order, with a few rules:
- The first time value is the duration. The second time value is the delay.
- Keywords such as
ease-in-out,infinite,alternate,forwardsandpausedare recognized by what they are, not by position. - The animation name can be any identifier that does not collide with another keyword in the list. Avoid naming keyframes
infinite,both,noneand similar words. - Multiple animations are separated by commas, and each longhand property then takes a matching comma-separated list.
.card {
animation:
slide-in 400ms ease-out both,
pulse 2s ease-in-out 1s infinite;
}The shorthand has one behavior you must remember for the rest of this tutorial. It resets every animation longhand it covers, and in current browsers that includes animation-timeline and animation-range. If you set animation-timeline first and then write an animation shorthand later in the same rule, the shorthand puts the timeline back to auto, and the animation silently goes back to running on time.
/* Wrong: the shorthand resets animation-timeline to auto */
.bar {
animation-timeline: scroll(root block);
animation: grow linear both;
}
/* Right: the shorthand comes first, then the timeline */
.bar {
animation: grow linear both;
animation-timeline: scroll(root block);
}Always declare animation-timeline and animation-range after the shorthand.
Easing, direction and iterations
animation-timing-function shapes progress between keyframes. Values include linear, ease, ease-in, ease-out, ease-in-out, cubic-bezier(...) and steps(...). Easing is applied between each pair of adjacent keyframes by default, not once across the whole animation. A keyframe can also set its own animation-timing-function to control the segment that starts at that keyframe.
@keyframes bounce {
0% { transform: translateY(0); animation-timing-function: ease-out; }
50% { transform: translateY(-40px); animation-timing-function: ease-in; }
100% { transform: translateY(0); }
}animation-direction has four values:
normalplays from 0% to 100%.reverseplays from 100% to 0%.alternategoes forward on odd iterations and backward on even ones.alternate-reverseis the opposite ofalternate.
animation-iteration-count accepts a number, including fractions such as 0.5, or infinite.
Fill mode
An animation has an active period, and your element exists before and after it. animation-fill-mode decides whether the keyframe styles apply outside that period.
| Value | Before the animation starts | After it ends |
|---|---|---|
| `none` | Normal styles | Normal styles |
| `forwards` | Normal styles | Last keyframe values |
| `backwards` | First keyframe values (during any delay) | Normal styles |
| `both` | First keyframe values | Last keyframe values |
Here is an example where the difference is visible:
@keyframes fade-up {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: none; }
}
.note {
animation: fade-up 600ms ease-out 1s both;
}With both, the note is invisible during the one-second delay, because the from values apply. It then animates, and it stays fully visible afterward. With none, the note would flash at full opacity for a second, then jump to invisible, animate in, and finally return to its normal styles.
Fill mode becomes more important with scroll-driven animations, where an element can sit outside the active part of the timeline for most of its life.
Where progress comes from
Every animation has a timeline. A timeline is an object that reports how far along the animation should be at this moment. By default, the timeline is the document timeline, which is a clock that starts when the page loads. The browser converts elapsed time into progress using the duration, delay, iteration count and direction.
The timeline is the only input you have not been choosing explicitly. With animation-timeline: auto, it is the clock. The next chapters replace it with a timeline based on scroll position. The keyframes, easing and fill mode stay the same. What changes is the meaning of duration. A time-driven animation lasts 600 milliseconds. A scroll-driven animation lasts as long as the scroll distance it is mapped to, so the animation-duration value no longer controls speed. Because browsers still need a duration value in the shorthand to parse it correctly, you will often see linear and both used without a time, and the duration treated as auto.
Common errors to check first
When an animation does not behave, run through this list before debugging anything exotic:
- The
@keyframesname inanimation-namemust match exactly. Names are case-sensitive. - Time-driven animations need a non-zero
animation-duration. - The animated property must be interpolable. Check that you are not animating
displayor a value that jumps between unrelated types. - A later rule or the shorthand may override a longhand through the cascade. Inspect the computed values in DevTools.
- Fill mode may be missing, which makes the element snap back to its normal styles at the end.
- Transforms on an element that is
display: inlinedo not apply. Useinline-block,block, or a flex or grid item.
Exercise
A rule sets animation-timeline: view(); and then, on the next line, animation: reveal linear both;. The element animates over time instead of with scroll. Why, and how do you fix it?
Show answer
The animation shorthand resets animation-timeline to its initial value auto, which means the document timeline, so the animation runs on the clock. Move animation-timeline: view(); below the shorthand so it is not reset.
Exercise
An element uses animation: fade-up 600ms ease-out 1s; where fade-up goes from opacity: 0 to opacity: 1. What does the user see, and which fill mode value would give the intended result of "invisible, then fade in, then stay visible"?
Show answer
With the default fill mode none, the element is fully visible during the one-second delay. It then jumps to invisible, fades in, and ends at its normal styles. Setting animation-fill-mode: both, or adding both to the shorthand, keeps the from values during the delay and the to values after the end.