Sub topic 1
Recap: keyframes and the animation properties
Scroll-driven animations reuse the CSS animation system you already know. Only the thing that drives progress changes. This chapter refreshes the parts you will rely on in later chapters.
Prerequisites
This chapter assumes you know:
- CSS selectors and the cascade, including how a later declaration overrides an earlier one.
- Basic
@keyframessyntax. - How
overflowcreates a scroll container. transformandopacity.
The working model
A CSS animation has two parts that you write separately:
- An
@keyframesrule describes what changes: a named list of styles at points along the animation. - The
animation-*properties on an element describe how to play it: which keyframes, how long, how fast, and what to do before and after.
The browser reads the keyframes and moves the element through those styles as the animation progresses. By default, progress comes from elapsed time. The next chapter replaces that source of progress with scroll position.
@keyframes pulse {
from { transform: scale(1); opacity: 1; }
to { transform: scale(1.2); opacity: 0.6; }
}
.badge {
animation-name: pulse;
animation-duration: 1.5s;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
animation-direction: alternate;
}The shorthand version is shorter:
.badge {
animation: pulse 1.5s ease-in-out infinite alternate;
}You can use percentages instead of from and to, and you can add as many stops as you need, for example 0%, 40% and 100%. If a property is missing from a stop, the browser uses the element's normal value for it.
The properties you will keep using
| Property | What it controls | Default |
|---|---|---|
| `animation-name` | Which `@keyframes` to play | `none` |
| `animation-duration` | Length of one cycle | `0s` |
| `animation-timing-function` | Easing between keyframes | `ease` |
| `animation-delay` | Wait before starting | `0s` |
| `animation-iteration-count` | How many cycles | `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` |
Fill mode matters most for what follows. With none, the element snaps back to its normal style when the animation ends. With both, it holds the first keyframe before the animation starts and the last keyframe after it ends.
Common errors
- No visible animation. The default duration is
0s, so an animation with no duration finishes instantly. Always set one. - A longhand that gets overwritten. The
animationshorthand resets everyanimation-*property, including ones you did not write. If you set a longhand earlier in the rule and then use the shorthand, the shorthand wins. Put longhands after the shorthand. This rule applies directly toanimation-timeline, which you will meet shortly. - Misspelled keyframe name. The name in
animation-namemust match the@keyframesname exactly. A mismatch fails silently. - Snapping back at the end. If the final state should stay, set
animation-fill-mode: forwardsorboth. - Animating expensive properties. Animating
width,topormarginforces layout work on every frame. Prefertransformandopacity.
Why this recap matters
In a scroll-driven animation, duration no longer means seconds. Progress is tied to scroll distance, so the timing function and fill mode still apply, but the duration is effectively ignored. You will write animation: grow linear both; with no time value and add a new property, animation-timeline, after it.
Exercise
A rule contains animation-timing-function: linear; animation: fade 2s;. Which easing does the animation use, and why?
Show answer
It uses the default ease. The animation shorthand comes later in the rule and resets animation-timing-function to its initial value, so the earlier linear is lost. To keep linear, put it in the shorthand (animation: fade 2s linear;) or declare the longhand after the shorthand.
Exercise
An element fades in with animation: reveal 1s; but turns invisible again as soon as the animation ends. What one change keeps it visible?
Show answer
Add a fill mode that holds the final keyframe, such as animation: reveal 1s forwards; or both. With the default none, the element returns to its non-animated style when the animation finishes.