Da Nang, VN,--:--:--
All articles
  • #react
  • #motion

Building a marquee that reacts to scroll velocity

Infinite tickers are everywhere. Here's how to make one that speeds up, reverses and skews with the user's scroll — in about 40 lines of React.

NDNgoc Duc1 min read

The scrolling ribbons on my home page aren't CSS animations. They're driven by requestAnimationFrame, so they can respond to how fast — and in which direction — you scroll. It's a small detail, but it makes the page feel alive.

The trick: render the content twice

An infinite marquee is an illusion. Put two identical copies side by side, move the track left, and as soon as you've moved by exactly one copy's width, jump back to zero. Because both halves are identical, the jump is invisible.

tsx
<div className="overflow-hidden">
  <div ref={track} className="flex w-max">
    <div className="flex">{children}</div>
    <div className="flex" aria-hidden>
      {children}
    </div>
  </div>
</div>

Note the aria-hidden on the duplicate — screen readers should only read the content once.

The loop

Each frame, we move by a base speed plus a boost from scroll velocity, then wrap around:

ts
const loop = () => {
  const v = scrollState.velocity; // from Lenis
  if (Math.abs(v) > 0.5) dir = v > 0 ? 1 : -1;
 
  x -= (speed + Math.abs(v) * boost) * dir;
  if (x <= -half) x += half;
  if (x > 0) x -= half;
 
  const skew = Math.max(-8, Math.min(8, v * 0.25));
  el.style.transform = `translate3d(${x}px,0,0) skewX(${-skew}deg)`;
  raf = requestAnimationFrame(loop);
};

Three things are happening:

  • Direction follows the last scroll direction, so scrolling up runs the ribbon backwards.
  • Speed is the base speed plus the absolute velocity.
  • Skew is clamped so fast flicks don't turn the text into spaghetti.

Measuring the half width

The wrap point is half of the track's scrollWidth. Fonts load late and viewports resize, so measure it with a ResizeObserver rather than once on mount:

ts
let half = el.scrollWidth / 2;
const ro = new ResizeObserver(() => (half = el.scrollWidth / 2));
ro.observe(el);

Respect reduced motion

Some people get dizzy from moving content. If prefers-reduced-motion is set, skip the loop entirely and let the ribbon sit still:

ts
if (matchMedia("(prefers-reduced-motion: reduce)").matches) return;

That's it — a few lines of math for a lot of personality.

Got a project in mind?

Let's talk