Scroll fades
A scroll area should fade only at the edge that has more content, and a CSS scroll timeline can do that with no script.
The problem
Fading the edge of a scroll area tells you there’s more. But a fade that’s always on lies at the ends: at the top there’s nothing above to fade into, and at the bottom the last row looks cut off.
The scripted fix listens to scroll and toggles classes, which is a listener per list and work on every scroll event.
What I concluded
A scroll timeline ties an animation to scroll position. Two registered lengths feed a mask. The top fade grows from zero as you leave the top, and the bottom fade shrinks to zero as you reach the end.
@property --fade-top {
syntax: "<length>"; inherits: false; initial-value: 0px; }
@property --fade-bottom {
syntax: "<length>"; inherits: false; initial-value: 40px; }
.list {
overflow-y: auto;
mask-image: linear-gradient(to bottom, transparent,
#000 var(--fade-top),
#000 calc(100% - var(--fade-bottom)), transparent);
}
@supports (animation-timeline: scroll()) {
.list {
animation: fade-top linear both, fade-bottom linear both;
animation-timeline: scroll(self y), scroll(self y);
animation-range: 0 96px, calc(100% - 96px) 100%;
}
}
The keyframes just move each length between zero and the fade size.
Rules
- Register the lengths. Without
@propertythe keyframes can’t interpolate them. - Guard with
@supports. Otherwise a browser without scroll timelines runs the animations instantly and parks them on the end frame: a fade at the top and none at the bottom, exactly backwards. - The fallback is the initial values. A static bottom fade and a crisp top, which suits a list that starts at the top.
- No listeners. The browser drives it off the compositor.
What you get
Fades that tell the truth about where the content is, at no runtime cost. Unsupported browsers get a sensible static version instead of a broken one.
The demo lets you scroll a list and compare with a static fade. Related: Theme switch.