Sub topic 2
Choosing the Scroller and Axis
Prerequisites: CSS selectors, @keyframes, animation properties, overflow, transforms, and opacity. A scroll-driven animation uses scroll position instead of elapsed clock time.
Choosing the scroller and the axis
A scroll progress timeline tracks how far a scroll container has moved. The scroll() function selects its source and axis:
scroll(root block)tracks the page’s root scroller vertically.scroll(nearest inline)tracks the nearest scrolling ancestor horizontally.scroll()uses the nearest scroller and its block axis by default.
The chosen scroller must actually scroll. If an element has no overflow, its timeline cannot progress.
View progress with `view()`
A view progress timeline tracks a subject as it moves through a scrollport. A scrollport is the visible area of a scrolling element. The subject’s timeline progresses as it enters, crosses, and leaves that area.
@keyframes reveal {
from {
opacity: 0;
transform: translateY(40px);
}
to {
opacity: 1;
transform: none;
}
}
.card {
animation: reveal 1s linear both;
animation-timeline: view(block);
animation-range: entry 0% cover 30%;
}Here, each .card uses its nearest scrolling ancestor. The animation starts when the card begins entering the scrollport and reaches its final state when the card has covered 30% of the scrollport. view(inline) would track horizontal movement instead.
Limiting the active range
animation-range maps only a selected part of a timeline to the keyframes. Common range names include:
entry: the subject enters the scrollport.cover: the subject travels across the scrollport.exit: the subject leaves the scrollport.
For example, entry 0% cover 30% avoids starting the animation too early or keeping it active for the entire journey. If you omit the range, the animation uses the timeline’s default range.
Easing, duration, and fill mode
The timeline determines progress. animation-duration still provides the animation’s nominal duration, so 1s is a clear convention. Easing changes how progress maps to the keyframes. linear usually feels predictable for scroll-linked motion.
both applies the first and last keyframe outside the active range. Without it, the element may return to its unanimated style before entry or after the range ends.
Animate transform and opacity when possible. They usually avoid expensive layout recalculation and keep scrolling smooth.
Exercise
Which declaration makes a card animate as it enters its nearest scrollport, and when does entry 0% cover 30% finish?
Show answer
Use animation-timeline: view();. The range finishes when the card has covered 30% of the scrollport.