Sub topic 13
Common patterns and pitfalls
This chapter assumes you know @keyframes, the animation properties, and how scroll(), view() and animation-range work from the earlier chapters. It covers the patterns you will reuse most and the mistakes that cause most "my animation does nothing" reports.
Three patterns to reuse
- Reading progress bar. A fixed element scales from 0 to 1 using
scroll(root block). Theanimationshorthand goes first andanimation-timelinegoes after it. - Reveal on entry. Each card uses its own
view()timeline. Limit the range toentry, so the animation finishes while the card is still arriving. - Scroll indicator outside the scroller. Name the timeline on the scroller and add
timeline-scopeon a shared ancestor.
Here is the reveal pattern in its safe form. It runs only when the browser supports scroll timelines and the user has not asked for reduced motion.
@keyframes reveal {
from { opacity: 0; transform: translateY(40px); }
to { opacity: 1; transform: none; }
}
@media (prefers-reduced-motion: no-preference) {
@supports (animation-timeline: view()) {
.card {
animation: reveal linear both;
animation-timeline: view();
animation-range: entry 0% entry 100%;
}
}
}Outside this block, .card has no animation, so it stays fully visible. That is the fallback you want.
Pitfalls
| Symptom | Cause | Fix |
|---|---|---|
| Animation never runs | `animation` shorthand written after `animation-timeline` reset it to `auto` | Declare `animation-timeline` last |
| Element flashes at its end state, then jumps | No fill mode, so the keyframes do not apply outside the active range | Use `both` |
| Motion feels eased or uneven | Default `ease` applied to scroll progress | Use `linear` and shape motion in the keyframes |
| Named timeline is ignored | The element using it is not a descendant of the scroller | Add `timeline-scope` on a common ancestor |
| Timeline never advances | The scroller has no overflow, or `overflow: hidden` removed the scrolling | Check that the element actually scrolls |
| Last card never finishes | Elements near the page end cannot scroll through the whole `cover` range | End the range at `entry 100%` |
| Janky, stuttering motion | Animating `width`, `top` or `margin` forces layout | Animate `transform` and `opacity` |
The fill mode and easing rows trip up people used to time-driven animation. With a scroll timeline the user controls the pace, so a time-based easing curve makes the motion feel out of step with the scrollbar. Keep the animation linear.
Do not rely on a time duration. A scroll-driven animation's duration is auto, which means it spans the active range. Leave the duration out of the shorthand.
Checking the working model
When an animation misbehaves, check these in order:
- Does the browser support
animation-timeline? - Is
animation-timelinedeclared after theanimationshorthand? - Is the scroller scrollable on the axis you chose?
- Is the timeline name in scope for the element that uses it?
- Does the range fit the element's position on the page?
@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);
}If this bar stays at zero width, the root scroller probably has no overflow. Also check that no ancestor rule sets animation after this declaration.
Exercise
A card uses animation-timeline: view() and animation: reveal linear both; in that order. The animation never plays. What is wrong, and how do you fix it?
Show answer
The animation shorthand comes after animation-timeline and resets it to auto, so the animation falls back to a time-based timeline. Move animation-timeline: view(); below the animation shorthand.