kipluo
React7 min read2 steps

How to Create a Scroll-Driven Animation

Scroll-linked motion used to require heavy scroll listeners that dropped frames. Learn when to use native CSS Scroll-Driven Animations and how to write a clean passive rAF fallback in React.

You needReact 18+CSS TransformsIntersectionObserver / Scroll progress
Loading demo
Continuous transform-driven motion that responds smoothly to pointer speed and pauses under reduced motion.Component page

1. Start with native CSS animation-timeline when possible

For reading progress bars and reveal-on-scroll headers, CSS Scroll-Driven Animations run completely off the main thread—zero JavaScript and zero React renders.

ReadingProgress.css
.reading-progress {
  position: fixed;
  inset: 0 0 auto 0;
  height: 3px;
  transform-origin: 0 50%;
  background: currentColor;
  animation: grow-bar linear both;
  animation-timeline: scroll(root block);
}

@keyframes grow-bar {
  from { transform: scaleX(0); }
  to   { transform: scaleX(1); }
}

@media (prefers-reduced-motion: reduce) {
  .reading-progress { animation: none; }
}

2. Write a spring-smoothed scroll progress fallback in React

When you need cross-browser support or want to feed scroll progress into a spring-smoothed transform (like parallax cards or velocity-reactive marquees), listen passively and write straight to a DOM ref inside requestAnimationFrame—never call setState on every scroll pixel.

useScrollProgress.ts
import { useEffect, useRef } from "react";

export function useScrollBar<T extends HTMLElement>() {
  const barRef = useRef<T>(null);

  useEffect(() => {
    const el = barRef.current;
    if (!el) return;
    let raf = 0;

    const update = () => {
      raf = 0;
      const max = Math.max(1, document.documentElement.scrollHeight - window.innerHeight);
      const pct = Math.min(1, Math.max(0, window.scrollY / max));
      el.style.transform = `scaleX(${pct.toFixed(4)})`;
    };

    const onScroll = () => {
      if (!raf) raf = requestAnimationFrame(update);
    };

    update();
    window.addEventListener("scroll", onScroll, { passive: true });
    return () => {
      cancelAnimationFrame(raf);
      window.removeEventListener("scroll", onScroll);
    };
  }, []);

  return barRef;
}

Accessibility checklist

Tick each one off as you check your build. Your ticks stay in this browser.

0 of 3 checked

Build it yourself or install it

Build it yourself when

Reading progress bars and simple sticky header transitions are ideal for pure CSS animation-timeline.

Install Kipluo when

When users also grab, drag, or fling continuous strips (like Infinite Marquee or Pull to Refresh), spring velocity handoff keeps scroll and pointer gestures seamless.

Keep building