/ 3 min read
Scroll-linked animation with one CSS variable and no library
How the icons on the Uses page are drawn by the scroll: JavaScript measures one number per element, CSS turns it into opacity and scale with clamp(), and reduced motion is respected. Small, fast and easy to test.
In this article
Scroll-linked animation usually arrives with a library and a timeline object. For a handful of elements you do not need either. On the Uses page of this site each icon is drawn dot by dot as you scroll, and the whole mechanism is one CSS variable per icon, set from a scroll listener, and four lines of CSS.
The idea: JavaScript measures, CSS decides
JavaScript does one job: turn "where is this element on the screen" into a number between 0 and 1 and write it to a custom property called --p. Everything about how that number looks lives in the stylesheet, so changing the effect never touches the script.
const update = () => {
const vh = innerHeight
for (const icon of icons) {
const top = icon.getBoundingClientRect().top
// 0 as the row enters at the bottom, 1 a little under halfway up.
const p = Math.min(1, Math.max(0, (vh * 0.96 - top) / (vh * 0.42)))
icon.style.setProperty('--p', p.toFixed(3))
}
}Turning one number into many dots
An icon is a set of dots, and each dot knows where it comes in. When the icon is built, every dot gets a variable --o between 0 and 0.85 that records its place in the drawing order. A dot is visible once --p passes its --o, and clamp() keeps the result between fully hidden and fully shown.
.dot-icon[data-scrub] .dot-icon-on {
opacity: clamp(0, calc((var(--p, 1) - var(--o)) * 8), 1);
transform: scale(clamp(0.2, calc(0.2 + (var(--p, 1) - var(--o)) * 8), 1));
}The fallback in var(--p, 1) matters. Before the script runs, or if it never does, the variable does not exist and the icon is drawn in full. The page works without JavaScript and the animation is an enhancement.
Why this is cheap
- The scroll listener is passive and does nothing but schedule one requestAnimationFrame, so several scroll events in a frame cost one update.
- Each update reads one rectangle per icon and writes one property. There is no layout thrash because all reads happen before the writes, and the writes do not change layout.
- The browser animates opacity and transform, the two properties that do not need layout. The dots are never moved by script.
The big icon follows the middle of the screen
The same trick drives the large icon beside the list. Each frame the script finds the row nearest the middle line of the screen and selects that tool. How far the row centre is from the line, measured in row heights, becomes its progress: empty as the row arrives, complete at the middle, undrawn as it leaves.
const t = (rect.top + rect.height / 2 - vh * 0.5) / rect.height
const p = Math.min(1, Math.max(0, 1 - Math.abs(t) * 1.7))
big.style.setProperty('--p', p.toFixed(3))Two edge cases needed care. Above the first row and below the last one, and at the very end of the page, the icon must stay complete, or a visitor at the top of the page sees an empty frame. And when someone picks a tool with the mouse or keyboard, the icon is drawn in full until the next scroll hands control back to the page.
Respecting reduced motion
The effect is skipped entirely when the visitor asks for reduced motion: the script returns early, never sets --p, and the CSS fallback shows every icon complete. Nothing hides, nothing animates.
How I tested it
A visual effect still needs a test. A Playwright script scrolls to a series of positions and reads the computed opacity of every dot, counting how many are visible. While scrolling through the list, the count for one icon went from 37 of 69 dots to 53 of 69, and the selected tool changed along the way. That is a number you can assert on, which is better than a screenshot you have to look at.
The same test caught a bug I would have missed by eye: two siblings in the detail panel shared a React key, so after a change both the old and the new icon were on the page. Counting the icons in the panel showed two instead of one.
When to reach for a library instead
This approach fits a few dozen elements with simple progress. If you need pinned sections, scrubbing a long timeline, or hundreds of independent animations, a dedicated library earns its weight. For a list of icons, one variable and a few lines of CSS are easier to read, easier to test and nothing to download. You can see it working on the Uses page.
Next article ↗
Dot matrix icons from code: 19 SVG icons on a 13 by 13 grid