Video scrubbing on scroll

Scroll-scrubbed video is the Apple product-page effect: the reader controls playback with the scroll wheel. Mechanically it is just video.currentTime driven by scroll progress, and the parts that are actually fiddly are preloading enough of the file to scrub without stalling and handling browsers that need Range requests. scrollVideo covers those.

014 steps

Build it, step by step

  1. Step 01 · terminal

    Encode the film for scrubbing

    Every scroll frame is a seek, and a seek decodes from the last keyframe. Exports usually put one every few seconds; for scrubbing, put one every few frames. Measured in Chromium on the same 5-second film: with a keyframe every 3 frames no seek took longer than 7.3 ms, with one keyframe for the whole clip seeks took up to 53.6 ms, three frames late. -an drops the audio, and -auto-alt-ref 0 turns off alternate reference frames, which tripled seek times when the Aural film was encoded.

    terminal2 lines
    ffmpeg -i film.mov -an -c:v libvpx -cpu-used 1 -pix_fmt yuv420p \
      -g 3 -auto-alt-ref 0 -b:v 2400k -crf 4 -qmin 0 -qmax 26 film.webm
    Fig. 1.1What a seek costs
    a keyframe every 3 frames90th percentile 6.5 ms · worst 7.3 msone keyframe for the whole clip90th percentile 48.8 ms · worst 53.6 msone frame, 16.7 ms0 ms20 ms40 ms60 ms
  2. Step 02 · film-scrub.css

    Give the scroll room to a track

    The stage is position: sticky inside a track four screens tall, so it stays stuck for three screens of scroll, and that stretch is the film. Keep the stage to exactly one screen, so nothing in it is ever out of reach.

    film-scrub.css8 lines
    .film-track { position: relative; height: 400vh; }
    
    .film-stage {
      position: sticky;
      top: 0;
      height: 100vh;
      height: 100svh;
    }
    Fig. 1.2Track and stage
    .film-track400vhstagestagestuck300vh= the film
  3. Step 03 · FilmScrub.tsx

    Measure the track, not the video

    scrollVideo measures the video unless told otherwise, and a video in a sticky stage moves with the scroll, so its window comes out wrong, and a resize mid-pin measures it wrong again. triggerElement (from 2.18.0) measures the track instead: top top to bottom bottom is exactly the stretch the stage is stuck.

    FilmScrub.tsx7 lines
    const film = scrollVideo(video.current, {
      triggerElement: track.current,
      trigger: { start: 'top top', end: 'bottom bottom' },
      onProgress: (p) => setChapter(chapterAt(p)),
    });
    
    return () => film.destroy();
    Fig. 1.3Which box sets the window
    videomeasure the videomoves with the scrollvideomeasure the trackthe whole pindashed: the track · bracket: the window
  4. Step 04 · FilmScrub.tsx

    Let the film pick the words

    onProgress reports how far through the film the reader is, 0 to 1. Give each chapter the point it takes over from, and show the last one that has started. The copy follows the film, so moving a chapter is one number.

    FilmScrub.tsx8 lines
    const CHAPTERS = [
      { from: 0,    title: 'Built to be worn all day', … },
      { from: 0.45, title: 'Taken apart', … },
      { from: 0.75, title: 'The part you hear', … },
    ];
    
    const chapterAt = (p) =>
      CHAPTERS.reduce((at, c, i) => (p >= c.from ? i : at), 0);

    Reduced motion shows the film’s last frame, so the chapters cannot be scrolled through; the CSS shows all of them instead.

    Fig. 1.4Chapters on the film
    010020.45030.751the copy follows the film, not the page
02The whole file

The complete component

Two files, ready to paste into a React or Next.js project with svg-scroll-draw installed. The film below them is this exact component, running on the Aural product film, which has colourway stills after the film in the same file, so this page passes to={4.98}.

FilmScrub.tsx54 lines
'use client';

import { useEffect, useRef, useState } from 'react';
import { scrollVideo } from 'svg-scroll-draw/video';
import './film-scrub.css';

/** What the copy says, and from which point in the film (0–1) it says it. */
const CHAPTERS = [
  { from: 0, title: 'Built to be worn all day', body: 'A sprung steel band and memory-foam cushions, 254 g all in.' },
  { from: 0.45, title: 'Taken apart', body: 'The cup comes apart layer by layer: the cap, the shell, the board.' },
  { from: 0.75, title: 'The part you hear', body: 'A 40 mm driver with a beryllium-coated dome, 4 Hz to 40 kHz.' },
];

const chapterAt = (p: number) => CHAPTERS.reduce((at, c, i) => (p >= c.from ? i : at), 0);

/**
 * `src`: a muted film encoded for scrubbing (a keyframe every few frames).
 * `to`: where the scrub stops, in seconds; leave it out to use the whole file.
 */
export function FilmScrub({ src = '/film.webm', to }: { src?: string; to?: number }) {
  const track = useRef<HTMLElement>(null);
  const video = useRef<HTMLVideoElement>(null);
  const [chapter, setChapter] = useState(0);

  useEffect(() => {
    if (!track.current || !video.current) return;
    const film = scrollVideo(video.current, {
      to,
      // The stage is sticky, so the video's own box moves with the scroll.
      // Measure the track that holds the scroll room instead.
      triggerElement: track.current,
      trigger: { start: 'top top', end: 'bottom bottom' },
      onProgress: (p) => setChapter(chapterAt(p)),
    });
    return () => film.destroy();
  }, [to]);

  return (
    <section ref={track} className="film-track">
      <div className="film-stage">
        <video ref={video} src={src} muted playsInline preload="auto" aria-label="The headphones turning, then taken apart" />
        <ol className="film-copy">
          {CHAPTERS.map((c, i) => (
            <li key={c.title} aria-current={i === chapter ? 'step' : undefined}>
              <span className="film-n">0{i + 1}</span>
              <h3>{c.title}</h3>
              <p>{c.body}</p>
            </li>
          ))}
        </ol>
      </div>
    </section>
  );
}
film-scrub.css49 lines
/* The track holds the scroll room: the stage stays stuck for its height minus
   one screen, and the film runs exactly that long. */
.film-track {
  --ink: #ece8e1;
  --paper: #121212;
  --accent: #d0a15c;
  position: relative;
  height: 400vh;
  background: var(--paper);
  color: var(--ink);
}

.film-stage {
  position: sticky;
  top: 0;
  box-sizing: border-box;
  display: grid;
  grid-template-columns: minmax(0, 1.3fr) minmax(0, 1fr);
  align-items: center;
  gap: 48px;
  height: 100vh;
  height: 100svh;
  padding: 0 6vw;
}
.film-stage video { display: block; width: 100%; height: auto; aspect-ratio: 4 / 3; object-fit: cover; }

.film-copy { display: grid; gap: 28px; margin: 0; padding: 0; list-style: none; }
/* Not the current chapter: greyed by colour, not opacity, so it keeps WCAG AA
   (#817f7c is 4.7:1 on the film's ground, the dimmed gold 4.65:1). */
.film-copy li { color: #817f7c; transition: color 0.3s ease; }
.film-copy li[aria-current] { color: var(--ink); }
.film-copy li:not([aria-current]) .film-n { color: #9b7947; }
.film-n { font: 600 12px/1 ui-monospace, monospace; color: var(--accent); }
.film-copy h3 { margin: 8px 0 8px; font: 600 24px/1.2 system-ui, sans-serif; }
.film-copy p { max-width: 34ch; margin: 0; font: 400 16px/1.6 system-ui, sans-serif; }

/* On a phone the stage still has to fit one screen: one chapter at a time. */
@media (max-width: 720px) {
  .film-stage { grid-template-columns: minmax(0, 1fr); align-content: center; gap: 20px; }
  .film-copy li:not([aria-current]) { display: none; }
}

/* Reduced motion shows the film's last frame, so the chapters cannot be
   scrolled through: show them all. */
@media (prefers-reduced-motion: reduce) {
  .film-copy li { color: var(--ink); transition: none; }
  .film-copy li:not([aria-current]) .film-n { color: var(--accent); }
  .film-copy li:not([aria-current]) { display: block; }
}
Fig. 2.1this file, running — keep scrolling
  1. 01

    Built to be worn all day

    A sprung steel band and memory-foam cushions, 254 g all in.

  2. 02

    Taken apart

    The cup comes apart layer by layer: the cap, the shell, the board.

  3. 03

    The part you hear

    A 40 mm driver with a beryllium-coated dome, 4 Hz to 40 kHz.

No build step

The same film as one HTML file. scrollVideo is not in the script-tag build, so this imports the ES module from a CDN. Serve it with film.webm beside it, from a server that answers Range requests.

index.html111 lines
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Scroll-scrubbed product film</title>
  <style>
    body { margin: 0; background: #121212; }
    .spacer { height: 100vh; }

    /* The track holds the scroll room: the stage stays stuck for its height minus
       one screen, and the film runs exactly that long. */
    .film-track {
      --ink: #ece8e1;
      --paper: #121212;
      --accent: #d0a15c;
      position: relative;
      height: 400vh;
      background: var(--paper);
      color: var(--ink);
    }

    .film-stage {
      position: sticky;
      top: 0;
      box-sizing: border-box;
      display: grid;
      grid-template-columns: minmax(0, 1.3fr) minmax(0, 1fr);
      align-items: center;
      gap: 48px;
      height: 100vh;
      height: 100svh;
      padding: 0 6vw;
    }
    .film-stage video { display: block; width: 100%; height: auto; aspect-ratio: 4 / 3; object-fit: cover; }

    .film-copy { display: grid; gap: 28px; margin: 0; padding: 0; list-style: none; }
    /* Not the current chapter: greyed by colour, not opacity, so it keeps WCAG AA
       (#817f7c is 4.7:1 on the film's ground, the dimmed gold 4.65:1). */
    .film-copy li { color: #817f7c; transition: color 0.3s ease; }
    .film-copy li[aria-current] { color: var(--ink); }
    .film-copy li:not([aria-current]) .film-n { color: #9b7947; }
    .film-n { font: 600 12px/1 ui-monospace, monospace; color: var(--accent); }
    .film-copy h3 { margin: 8px 0 6px; font: 600 24px/1.2 system-ui, sans-serif; }
    .film-copy p { max-width: 34ch; margin: 0; font: 400 16px/1.6 system-ui, sans-serif; }

    /* On a phone the stage still has to fit one screen: one chapter at a time. */
    @media (max-width: 720px) {
      .film-stage { grid-template-columns: minmax(0, 1fr); align-content: center; gap: 20px; }
      .film-copy li:not([aria-current]) { display: none; }
    }

    /* Reduced motion shows the film's last frame, so the chapters cannot be
       scrolled through: show them all. */
    @media (prefers-reduced-motion: reduce) {
      .film-copy li { color: var(--ink); transition: none; }
      .film-copy li:not([aria-current]) .film-n { color: var(--accent); }
      .film-copy li:not([aria-current]) { display: block; }
    }
  </style>
</head>
<body>
  <div class="spacer"></div>

  <section class="film-track" id="track">
    <div class="film-stage">
      <video id="film" src="film.webm" muted playsinline preload="auto" aria-label="The headphones turning, then taken apart"></video>
      <ol class="film-copy">
        <li aria-current="step" data-from="0">
          <span class="film-n">01</span>
          <h3>Built to be worn all day</h3>
          <p>A sprung steel band and memory-foam cushions, 254 g all in.</p>
        </li>
        <li data-from="0.45">
          <span class="film-n">02</span>
          <h3>Taken apart</h3>
          <p>The cup comes apart layer by layer: the cap, the shell, the board.</p>
        </li>
        <li data-from="0.75">
          <span class="film-n">03</span>
          <h3>The part you hear</h3>
          <p>A 40 mm driver with a beryllium-coated dome, 4 Hz to 40 kHz.</p>
        </li>
      </ol>
    </div>
  </section>

  <div class="spacer"></div>

  <script type="module">
    import { scrollVideo } from 'https://cdn.jsdelivr.net/npm/svg-scroll-draw@2.31.0/dist/video/index.mjs';

    const chapters = [...document.querySelectorAll('.film-copy li')];
    let shown = 0;

    scrollVideo(document.getElementById('film'), {
      // The stage is sticky, so the video's own box moves with the scroll.
      // Measure the track that holds the scroll room instead.
      triggerElement: document.getElementById('track'),
      trigger: { start: 'top top', end: 'bottom bottom' },
      onProgress: (p) => {
        const i = chapters.reduce((at, li, j) => (p >= Number(li.dataset.from) ? j : at), 0);
        if (i === shown) return;
        chapters[shown].removeAttribute('aria-current');
        chapters[i].setAttribute('aria-current', 'step');
        shown = i;
      },
    });
  </script>
</body>
</html>
035 problems

Where it goes wrong

01

It stutters, and worse further into the film

The film has few keyframes, so each seek decodes from far back. Re-encode with a keyframe every few frames (-g 3); measured, that kept every seek under 7.3 ms against up to 53.6 ms.

02

It does not scrub at all once deployed

The server ignores Range requests. Measured in Chromium against such a server: asked for 3 s, the film stayed at 0 s with nothing seekable, though the whole file had arrived. Check that the video comes back as 206 Partial Content with Accept-Ranges: bytes.

03

The film runs in the wrong stretch, or jumps after a resize

It is measuring the video, which moves while the stage is stuck, and a phone’s address bar showing or hiding is a resize. Measure the track with triggerElement.

04

A phone shows an empty box

That browser cannot play the file. Check video.canPlayType('video/webm; codecs="vp8"') on the oldest phone you support; if it comes back empty, add an MP4 <source> encoded with the same short keyframe interval, and a poster so there is a picture either way.

05

The stage scrolls away instead of sticking

An ancestor has overflow: hidden, auto or Scroll, and a sticky element sticks inside the nearest one of those, not the page. Remove it, or use overflow: clip, which does not create one.

17scrollVideo · to · onProgress

In a real site

The product page for a pair of over-ear headphones. A rendered product film scrubs with the scroll while its stage is pinned: the headphones turn, then the ear cup comes apart layer by layer, and onProgress pins a numbered callout to each part as it comes clear.

Aural, over-ear headphones, the aural one: the first screen of its demo site

Over-ear headphones, the Aural One

Aural

Live site

Related examples

Make your own in the scroll video scrub generator or the scroll image sequence generator: set it up, watch it run, copy the code.

MIT licensed, zero runtime dependencies. Every API is a separate entry point, so you only ship what you import. Full API reference →