← All posts

A staggered scroll animation in one CSS line

animation-range-start and sibling-index() stagger eight characters on scroll. The long custom formula was more than this tally needed.

A staggered scroll animation in one CSS line
Contents

In brief

Eight characters on a donation tally should appear in sequence as the page scrolls. A Frontend Masters walkthrough built that stagger with a long custom formula. The CSS-Tricks reply says the platform already has the lever. animation-range-start shifts when a scroll-driven animation begins, and sibling-index() supplies each character's place among its siblings.

animation-delay does not belong on this timeline. It applies to time-based animations.

What happened

Ashutosh Biswas, writing for Frontend Masters, staggered a scroll-driven animation with a custom formula and custom CSS functions. The reply treats that machinery as too much for a common effect.

The donation-tally demo has eight characters. Each one starts a step later:

animation-range-start: contain calc(10% + 2.5% * sibling-index());

The declaration has no custom functions. The step lives in calc(): 2.5% times the sibling index, added to 10%. The long expression drops away.

Eight glyphs sit in one line of the tally. Scroll brings each one to the start of its animation at its own moment. The step stays constant, so the queue stays even: the next glyph waits an extra 2.5% of the progress, and the eighth waits the longest. The index comes from the order in the markup. The declaration reads that order itself.

Easing is the other split. Merge the animations onto one master timeline and animation-timing-function eases that whole timeline. The characters do not each receive a curve. Standard easing does work on scroll-driven animations when you set it on the element itself.

He grants that rebuilding something the browser already does can be justified on other jobs, and names React, Pretext, and CSSTree. For this eight-character tally the custom formula was more machine than the effect needed. He still wants the other scene: a CSS animation that follows any single progress value. Scroll is only one source in that scene.

Why it matters

A karaoke line lights up word by word while one song plays. One progress value, staggered entrances. The author asks you to picture that scene in CSS: a ball bouncing along text, the highlight moving character by character, the whole scene tied to one progress value. In the picture, the value comes from speech recognition.

Scroll has the same shape, with the page as the source of progress. A custom formula hides the step in arithmetic. animation-range-start leaves the step in one declaration. Add a character to the markup, sibling-index() moves, and the offset follows.

A merged timeline also fights the easing you expect. You want each character to slow at the end of its own slice. On a shared timeline there is one slowdown for everyone. For an ordinary character queue, keep the scroll timeline and move only the start.

In practice

Look at where the progress comes from. Siblings under one parent, progress from scroll: one declaration is enough. Progress from outside, as in the imagined speech-recognition scene, sits outside the built-in scroll range. A timeline driven by an arbitrary progress value still has a point there. The article leaves that as a direction. It does not use the idea to replace the tally.

On the tally the declaration is shared. sibling-index() reads an element's place among the children of one parent. Move a glyph under another parent and its place among siblings changes, so the step moves with it. animation-delay does not repair that move. On a scroll timeline it does not shift the start.

Custom formula Built-in line
Offset Long expression and custom functions animation-range-start and sibling-index()
Easing On the whole merged timeline On each element
Eight-character tally More than the effect needs Even 2.5% steps

To rebuild the tally:

  1. Attach the animation to a scroll timeline. A timer will not move these starts.
  2. Shift the start with animation-range-start: contain calc(10% + 2.5% * sibling-index());.
  3. Leave animation-delay off this effect.
  4. Set the easing on each character.

Takeaway

For a queue on scroll, the browser already spaces the starts by sibling index. Write a formula when the whole scene has to follow one external progress value, the way the karaoke highlight follows speech. Characters that enter as the page moves need the one line.

Comments

Loading comments…