23 / blog / how-toJune 202612 min read

Horizontal scroll sections without GSAP

Apple does it. Stripe does it. Linear does it. Vertical scroll drives horizontal movement, and it's one of the most striking effects on the web. Here it is as a real component, a strip of case studies, built in 5 steps, with the files run in a browser and the 5 ways it goes wrong measured.

  • How-To
  • scrollHorizontal
  • svg-scroll-draw/horizontal
  • no GSAP
Setup
one call
Default trigger
top top → bottom bottom
Options
7
Every API
11.9 KB · MIT
Four full-width sections in a horizontal track, translated left by half the travel distance, under a sticky viewport; below, the outer container's scroll runway from top top to bottom bottom with the current position at p = 0.5..tracktranslateX(−p · distance)section 1section 2section 3section 4.sticky · 100vhdistance = track width − stage widthscrollY.outer · 400vhp = 0.5top topp = 0onProgress(p)linear = scrubbottom bottomp = 1
Fig. 1 — the three layers at p = 0.5: the track translated under the sticky stage, and the outer container's runway that drives it
01The idea

How it works

The pattern has three layers:

  1. A tall outer container — gives the page enough scroll height for the horizontal travel distance
  2. A sticky inner container — stays fixed in the viewport while the user scrolls through the outer
  3. A horizontal track — slides left (translateX) proportional to how far through the outer the user has scrolled

You set up the CSS. scrollHorizontal drives the translateX. To try panel counts, widths and scroll lengths before you write any of it, the horizontal scroll generator runs the same three layers in a box you scroll and copies the code.

025 steps

Build it, step by step

  1. Step 01 · case-strip.css

    Give it a runway, a stage and a track

    Three boxes. The runway, .work, is the scroll room. The stage inside it sticks to the top of the screen while the runway scrolls past. The track inside the stage is one row of cases, as wide as they are. Cases narrower than the stage leave the next one showing at the edge, which is how a reader knows the row goes on; sized in cqw, that holds in a page container as well as across the full screen.

    case-strip.css22 lines
    .work {
      height: calc(var(--travel, 200vh) + 100svh);
    }
    
    .work-stage {
      position: sticky;
      top: 0;
      height: 100svh;
      padding: clamp(24px, 7vh, 72px) 6vw;
      overflow: clip;
      container-type: inline-size;
    }
    
    /* As wide as its cases, so it can be measured and moved. */
    .work-track {
      display: flex;
      gap: 24px;
      width: max-content;
    }
    
    /* cqw: the stage's width, wherever the strip is placed. */
    .work-case { width: min(78cqw, 520px); }

    clip, not hidden: step 5 is why.

    Fig. 2.1The three boxes
    .work-tracktranslateX.work-stagesticky · clip.workthe runwaythe stage isstuck for this:the travel
  2. Step 02 · CaseStrip.tsx

    Move it with one call

    No distance and no trigger. scrollHorizontal walks up to the nearest position: sticky ancestor, takes the scroll window from that stage’s parent, the runway, and moves the track by its own width minus the stage’s content box. From 2.18.1 it measures again whenever the window, the page, the track or the runway changes size, so a phone turned sideways, or a cover image that loads late, does not leave the last case out of reach.

    CaseStrip.tsx7 lines
    import { scrollHorizontal } from 'svg-scroll-draw/horizontal';
    
    useEffect(() => {
      if (!track.current) return;
      const strip = scrollHorizontal(track.current);
      return () => strip.destroy();
    }, []);
    Fig. 2.2What the track is moved by
    measured at 1280 × 800track 2,152 pxstage 1,126 pxtravel 1,026 px
  3. Step 03 · CaseStrip.tsx

    Make the runway as long as the strip

    The stage stays stuck for the runway’s height minus one screen, and the strip crosses its whole travel in that time. A fixed 400vh runway sets the speed by accident: measured on this strip, 32.8 px sideways for every 100 px of scroll at 1440 wide and 31.6 px on a phone, so the strip crawls at a third of the page’s speed. Make the runway the travel plus one screen and every pixel of scroll moves it one pixel, at any width.

    CaseStrip.tsx7 lines
    // A pixel of scroll moves the strip a pixel: the runway is the strip's
    // travel plus the one screen the stage stays stuck for.
    const fit = () => runway.style.setProperty('--travel', `${travelOf(st, tr)}px`);
    fit();
    const ro = new ResizeObserver(fit);
    ro.observe(tr);
    ro.observe(st);

    Call fit() Before scrollHorizontal, so its first measurement already sees the right runway. After that the observer keeps the height current, and the strip follows the runway’s new height on its own.

    Fig. 2.3px sideways per 100 px of scroll
    400vh runway · 1440 × 90032.8 px400vh runway · 390 × 84431.6 pxtravel + one screen · both100 px050100px sideways per 100 px of scroll
  4. Step 04 · CaseStrip.tsx

    Show where you are

    onProgress gives you 0 to 1 across the travel. The rail is that number as a scale. The count rounds it to the nearest case, so with four cases it ticks over at 1/6, 1/2 and 5/6, and it is written only when it changes. Both are aria-hidden: the list already tells a screen reader how many cases there are.

    CaseStrip.tsx10 lines
    const strip = scrollHorizontal(tr, {
      onProgress: (p) => {
        if (fill.current) fill.current.style.transform = `scaleX(${p})`;
        const i = Math.round(p * (n - 1));
        if (i !== shown && count.current) {
          count.current.textContent = String(i + 1).padStart(2, '0');
          shown = i;
        }
      },
    });
    Fig. 2.4Where the count changes
    010021/6031/2045/61round(p × 3) + 1 · the rail is scaleX(p)
  5. Step 05 · CaseStrip.tsx

    Let the keyboard in

    Every case has a link, and Tab reaches all four, including the ones still off to the right. With overflow: hidden the browser scrolls the stage itself to show a focused link, on top of the transform, and nothing scrolls it back: measured on this strip, tabbing to the fourth case left the stage scrolled 615 px, so the start of the strip opened on case 2. overflow: clip cannot be scrolled at all. The handler scrolls the page to the point where that case is in view, and the strip’s own transform does the rest.

    CaseStrip.tsx12 lines
    const onFocus = (e: FocusEvent) => {
      const item = (e.target as Element).closest('li');
      if (!item) return;
      const travel = travelOf(st, tr);
      // How far into the track the case starts. Both boxes carry the same
      // translateX, so it cancels out.
      const at = item.getBoundingClientRect().left - tr.getBoundingClientRect().left;
      const p = travel > 0 ? Math.min(1, at / travel) : 0;
      const top = runway.getBoundingClientRect().top + window.scrollY + p * (runway.offsetHeight - window.innerHeight);
      requestAnimationFrame(() => window.scrollTo({ top }));
    };
    tr.addEventListener('focusin', onFocus);

    Two things that look simpler and are not. The scroll waits a frame: the browser runs its own scroll to the focused link after focusin and undoes one made inside the handler. And offsetLeft would read 0: once the library writes a transform, the track becomes the case’s offset parent.

    Fig. 2.5tab to case 4, back to the start · 1280 × 800
    overflow: hidden0203tab to case 4, thenback to the start:it opens on case 2scrollLeft 615 pxoverflow: clip + focusin0102tab to case 4: thepage scrolls to itback to the start:it opens on case 1
03The whole file

The complete component

Two files, ready to paste into a React or Next.js project with svg-scroll-draw installed. The component takes the cases as a prop; the runway, the count and the keyboard are all in its one effect. The strip below them is this exact component, running.

CaseStrip.tsx151 lines
'use client';

import { useEffect, useRef } from 'react';
import { scrollHorizontal } from 'svg-scroll-draw/horizontal';
import './case-strip.css';

export type Case = { client: string; title: string; text: string; meta: string; href: string; cover: string[] };

export const CASES: Case[] = [
  {
    client: 'Tidewater Ferries',
    title: 'A timetable you can read from the pier',
    text: 'Signs and a printed timetable for six crossings, set large enough to read with a bag in each hand.',
    meta: 'Wayfinding · 2025',
    href: '/work/tidewater-ferries',
    cover: [
      'M 40 92 L 160 92 L 148 108 L 52 108 Z',
      'M 70 92 V 76 H 128 V 92',
      'M 112 76 V 62 H 122 V 76',
      'M 20 122 q 10 -8 20 0 t 20 0 t 20 0 t 20 0 t 20 0 t 20 0 t 20 0 t 20 0',
    ],
  },
  {
    client: 'Hollow Oak Bakery',
    title: 'Boxes that survive the bike courier',
    text: 'A fold-flat box for loaves and tarts, tested on cobbles, that the bakery folds in eleven seconds.',
    meta: 'Packaging · 2025',
    href: '/work/hollow-oak-bakery',
    cover: [
      'M 60 62 L 100 46 L 140 62 L 140 108 L 100 124 L 60 108 Z',
      'M 60 62 L 100 78 L 140 62 M 100 78 V 124',
      'M 82 54 Q 100 34 118 54',
    ],
  },
  {
    client: 'Northfield Library',
    title: 'Late returns, without the guilt',
    text: 'Reminder letters and a returns page rewritten so that people come back instead of staying away.',
    meta: 'Service design · 2024',
    href: '/work/northfield-library',
    cover: [
      'M 58 50 V 112 H 74 V 50 Z',
      'M 78 42 V 112 H 92 V 42 Z',
      'M 100 56 L 113 52 L 130 110 L 117 114 Z',
      'M 44 112 H 156',
    ],
  },
  {
    client: 'Meridian Cycling Club',
    title: 'Route maps that fold into a jersey pocket',
    text: 'Twelve club routes on one sheet, with the climbs drawn to scale and the cafés where they matter.',
    meta: 'Print · 2024',
    href: '/work/meridian-cycling-club',
    cover: [
      'M 40 52 L 80 42 L 120 52 L 160 42 V 108 L 120 118 L 80 108 L 40 118 Z',
      'M 80 42 V 108 M 120 52 V 118',
      'M 52 100 C 70 80 90 96 104 76 S 138 72 148 58',
    ],
  },
];

/** How far the strip travels: its width minus the stage's content box. */
function travelOf(stage: HTMLElement, track: HTMLElement): number {
  const cs = getComputedStyle(stage);
  const seen = stage.clientWidth - parseFloat(cs.paddingLeft) - parseFloat(cs.paddingRight);
  return Math.max(0, track.scrollWidth - seen);
}

export function CaseStrip({ cases = CASES }: { cases?: Case[] }) {
  const work = useRef<HTMLElement>(null);
  const stage = useRef<HTMLDivElement>(null);
  const track = useRef<HTMLOListElement>(null);
  const count = useRef<HTMLElement>(null);
  const fill = useRef<HTMLSpanElement>(null);

  useEffect(() => {
    const runway = work.current, st = stage.current, tr = track.current;
    if (!runway || !st || !tr) return;
    const n = tr.children.length;

    // A pixel of scroll moves the strip a pixel: the runway is the strip's
    // travel plus the one screen the stage stays stuck for.
    const fit = () => runway.style.setProperty('--travel', `${travelOf(st, tr)}px`);
    fit();
    const ro = new ResizeObserver(fit);
    ro.observe(tr);
    ro.observe(st);

    let shown = 0;
    const strip = scrollHorizontal(tr, {
      onProgress: (p) => {
        if (fill.current) fill.current.style.transform = `scaleX(${p})`;
        const i = Math.round(p * (n - 1));
        if (i !== shown && count.current) {
          count.current.textContent = String(i + 1).padStart(2, '0');
          shown = i;
        }
      },
    });

    // The stage cannot scroll (overflow: clip), so a case reached with Tab is
    // brought into view by scrolling the page to it — on the next frame, after
    // the browser's own scroll to the focused link.
    const onFocus = (e: FocusEvent) => {
      const item = (e.target as Element).closest('li');
      if (!item) return;
      const travel = travelOf(st, tr);
      // How far into the track the case starts. Both boxes carry the same
      // translateX, so it cancels out.
      const at = item.getBoundingClientRect().left - tr.getBoundingClientRect().left;
      const p = travel > 0 ? Math.min(1, at / travel) : 0;
      const top = runway.getBoundingClientRect().top + window.scrollY + p * (runway.offsetHeight - window.innerHeight);
      requestAnimationFrame(() => window.scrollTo({ top }));
    };
    tr.addEventListener('focusin', onFocus);

    return () => {
      tr.removeEventListener('focusin', onFocus);
      ro.disconnect();
      strip.destroy();
    };
  }, [cases]);

  return (
    <section ref={work} className="work" aria-labelledby="work-h">
      <div ref={stage} className="work-stage">
        <header className="work-head">
          <h2 id="work-h" className="work-h">Selected work</h2>
          <p className="work-count" aria-hidden="true">
            <b ref={count}>01</b> / {String(cases.length).padStart(2, '0')}
          </p>
          <span className="work-rail" aria-hidden="true"><span ref={fill} /></span>
        </header>
        <ol ref={track} className="work-track">
          {cases.map((c, i) => (
            <li key={c.client} className="work-case">
              <svg className="work-cover" viewBox="0 0 200 150" aria-hidden="true">
                {c.cover.map((d) => <path key={d} d={d} />)}
              </svg>
              <p className="work-n">{String(i + 1).padStart(2, '0')} · {c.client}</p>
              <h3 className="work-title">{c.title}</h3>
              <p className="work-text">{c.text}</p>
              <p className="work-meta">{c.meta}</p>
              <a className="work-link" href={c.href}>Read the case study</a>
            </li>
          ))}
        </ol>
      </div>
    </section>
  );
}
case-strip.css81 lines
/* The runway: the strip's travel (set from the script) plus the one screen the
   stage stays stuck for, so a pixel of scroll moves the strip a pixel. */
.work {
  --ink: #161b22;
  --paper: #eceef1;
  --accent: #3056d3;
  --line: rgba(22, 27, 34, 0.16);
  height: calc(var(--travel, 200vh) + 100vh);
  height: calc(var(--travel, 200vh) + 100svh);
  background: var(--paper);
  color: var(--ink);
}

/* clip, not hidden: a hidden box can still be scrolled by focus, which shifts
   the whole strip and leaves it shifted. */
.work-stage {
  position: sticky;
  top: 0;
  box-sizing: border-box;
  display: grid;
  /* minmax(0, 1fr): an auto column would grow to the track's full width,
     and take the header with it. */
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr);
  gap: clamp(20px, 4vh, 40px);
  height: 100vh;
  height: 100svh;
  padding: clamp(24px, 7vh, 72px) 6vw;
  overflow: clip;
  container-type: inline-size;
}

.work-head { display: flex; align-items: center; gap: 16px; }
.work-h { margin: 0; font: 600 clamp(24px, 3vw, 36px)/1.1 system-ui, sans-serif; letter-spacing: -0.01em; }
.work-count { flex: none; margin: 0 0 0 auto; font: 500 13px/1 ui-monospace, monospace; font-variant-numeric: tabular-nums; white-space: nowrap; }
.work-rail { position: relative; flex: 0 0 min(24vw, 200px); height: 2px; background: var(--line); }
.work-rail span { position: absolute; inset: 0; background: var(--ink); transform: scaleX(0); transform-origin: left; }

/* As wide as its cases, so it can be measured and moved. */
.work-track {
  display: flex;
  gap: 24px;
  width: max-content;
  margin: 0;
  padding: 0;
  list-style: none;
}

/* Narrower than the stage (cqw: its width, wherever it is placed), so the
   next case shows at the edge. */
.work-case {
  display: flex;
  flex-direction: column;
  width: min(78cqw, 520px);
  min-height: 0;
}
.work-cover {
  box-sizing: border-box;
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  border: 1px solid var(--line);
  background: #fff;
}
.work-cover path { fill: none; stroke: var(--ink); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.work-n { margin: 16px 0 0; font: 600 12px/1 ui-monospace, monospace; color: var(--accent); }
.work-title { margin: 8px 0 8px; font: 600 clamp(19px, 2vw, 24px)/1.2 system-ui, sans-serif; }
.work-text { max-width: 44ch; margin: 0; font: 400 15px/1.55 system-ui, sans-serif; opacity: 0.8; }
/* 0.7: at 0.6 the meta line measured under WCAG AA's 4.5:1 (0.63 is the floor). */
.work-meta { margin: 8px 0 0; font: 400 13px/1.4 system-ui, sans-serif; opacity: 0.7; }
.work-link {
  align-self: flex-start;
  margin-top: 8px;
  font: 500 15px/1.4 system-ui, sans-serif;
  color: var(--ink);
  text-underline-offset: 3px;
}
.work-link:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }

/* html, body { overflow-x: hidden } would unstick the stage; if a reset needs
   to stop sideways scrolling, use overflow-x: clip there too. */
Fig. 3.1this file, running — keep scrolling

Selected work

  1. 01 · Tidewater Ferries

    A timetable you can read from the pier

    Signs and a printed timetable for six crossings, set large enough to read with a bag in each hand.

    Wayfinding · 2025

    Read the case study
  2. 02 · Hollow Oak Bakery

    Boxes that survive the bike courier

    A fold-flat box for loaves and tarts, tested on cobbles, that the bakery folds in eleven seconds.

    Packaging · 2025

    Read the case study
  3. 03 · Northfield Library

    Late returns, without the guilt

    Reminder letters and a returns page rewritten so that people come back instead of staying away.

    Service design · 2024

    Read the case study
  4. 04 · Meridian Cycling Club

    Route maps that fold into a jersey pocket

    Twelve club routes on one sheet, with the climbs drawn to scale and the cafés where they matter.

    Print · 2024

    Read the case study

No build step

The same strip as one HTML file, importing the ES module from a CDN. Save it and open it in a browser.

index.html213 lines
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Horizontal case-study strip</title>
  <style>
    body { margin: 0; background: #eceef1; }
    .spacer { height: 100vh; }

    /* The runway: the strip's travel (set from the script) plus the one screen the
       stage stays stuck for, so a pixel of scroll moves the strip a pixel. */
    .work {
      --ink: #161b22;
      --paper: #eceef1;
      --accent: #3056d3;
      --line: rgba(22, 27, 34, 0.16);
      height: calc(var(--travel, 200vh) + 100vh);
      height: calc(var(--travel, 200vh) + 100svh);
      background: var(--paper);
      color: var(--ink);
    }

    /* clip, not hidden: a hidden box can still be scrolled by focus, which shifts
       the whole strip and leaves it shifted. */
    .work-stage {
      position: sticky;
      top: 0;
      box-sizing: border-box;
      display: grid;
      /* minmax(0, 1fr): an auto column would grow to the track's full width,
         and take the header with it. */
      grid-template-columns: minmax(0, 1fr);
      grid-template-rows: auto minmax(0, 1fr);
      gap: clamp(20px, 4vh, 40px);
      height: 100vh;
      height: 100svh;
      padding: clamp(24px, 7vh, 72px) 6vw;
      overflow: clip;
      container-type: inline-size;
    }

    .work-head { display: flex; align-items: center; gap: 16px; }
    .work-h { margin: 0; font: 600 clamp(24px, 3vw, 36px)/1.1 system-ui, sans-serif; letter-spacing: -0.01em; }
    .work-count { flex: none; margin: 0 0 0 auto; font: 500 13px/1 ui-monospace, monospace; font-variant-numeric: tabular-nums; white-space: nowrap; }
    .work-rail { position: relative; flex: 0 0 min(24vw, 200px); height: 2px; background: var(--line); }
    .work-rail span { position: absolute; inset: 0; background: var(--ink); transform: scaleX(0); transform-origin: left; }

    /* As wide as its cases, so it can be measured and moved. */
    .work-track {
      display: flex;
      gap: 24px;
      width: max-content;
      margin: 0;
      padding: 0;
      list-style: none;
    }

    /* Narrower than the stage (cqw: its width, wherever it is placed), so the
       next case shows at the edge. */
    .work-case {
      display: flex;
      flex-direction: column;
      width: min(78cqw, 520px);
      min-height: 0;
    }
    .work-cover {
      box-sizing: border-box;
      flex: 1 1 auto;
      min-height: 0;
      width: 100%;
      border: 1px solid var(--line);
      background: #fff;
    }
    .work-cover path { fill: none; stroke: var(--ink); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
    .work-n { margin: 16px 0 0; font: 600 12px/1 ui-monospace, monospace; color: var(--accent); }
    .work-title { margin: 8px 0 6px; font: 600 clamp(19px, 2vw, 24px)/1.2 system-ui, sans-serif; }
    .work-text { max-width: 44ch; margin: 0; font: 400 15px/1.55 system-ui, sans-serif; opacity: 0.8; }
    .work-meta { margin: 8px 0 0; font: 400 13px/1.4 system-ui, sans-serif; opacity: 0.7; }
    .work-link {
      align-self: flex-start;
      margin-top: 10px;
      font: 500 15px/1.4 system-ui, sans-serif;
      color: var(--ink);
      text-underline-offset: 3px;
    }
    .work-link:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }

    /* html, body { overflow-x: hidden } would unstick the stage; if a reset needs
       to stop sideways scrolling, use overflow-x: clip there too. */
  </style>
</head>
<body>
  <div class="spacer"></div>

  <section class="work" id="work" aria-labelledby="work-h">
    <div class="work-stage">
      <header class="work-head">
        <h2 id="work-h" class="work-h">Selected work</h2>
        <p class="work-count" aria-hidden="true"><b>01</b> / 04</p>
        <span class="work-rail" aria-hidden="true"><span></span></span>
      </header>
      <ol class="work-track">
        <li class="work-case">
          <svg class="work-cover" viewBox="0 0 200 150" aria-hidden="true">
            <path d="M 40 92 L 160 92 L 148 108 L 52 108 Z" />
            <path d="M 70 92 V 76 H 128 V 92" />
            <path d="M 112 76 V 62 H 122 V 76" />
            <path d="M 20 122 q 10 -8 20 0 t 20 0 t 20 0 t 20 0 t 20 0 t 20 0 t 20 0 t 20 0" />
          </svg>
          <p class="work-n">01 · Tidewater Ferries</p>
          <h3 class="work-title">A timetable you can read from the pier</h3>
          <p class="work-text">Signs and a printed timetable for six crossings, set large enough to read with a bag in each hand.</p>
          <p class="work-meta">Wayfinding · 2025</p>
          <a class="work-link" href="/work/tidewater-ferries">Read the case study</a>
        </li>
        <li class="work-case">
          <svg class="work-cover" viewBox="0 0 200 150" aria-hidden="true">
            <path d="M 60 62 L 100 46 L 140 62 L 140 108 L 100 124 L 60 108 Z" />
            <path d="M 60 62 L 100 78 L 140 62 M 100 78 V 124" />
            <path d="M 82 54 Q 100 34 118 54" />
          </svg>
          <p class="work-n">02 · Hollow Oak Bakery</p>
          <h3 class="work-title">Boxes that survive the bike courier</h3>
          <p class="work-text">A fold-flat box for loaves and tarts, tested on cobbles, that the bakery folds in eleven seconds.</p>
          <p class="work-meta">Packaging · 2025</p>
          <a class="work-link" href="/work/hollow-oak-bakery">Read the case study</a>
        </li>
        <li class="work-case">
          <svg class="work-cover" viewBox="0 0 200 150" aria-hidden="true">
            <path d="M 58 50 V 112 H 74 V 50 Z" />
            <path d="M 78 42 V 112 H 92 V 42 Z" />
            <path d="M 100 56 L 113 52 L 130 110 L 117 114 Z" />
            <path d="M 44 112 H 156" />
          </svg>
          <p class="work-n">03 · Northfield Library</p>
          <h3 class="work-title">Late returns, without the guilt</h3>
          <p class="work-text">Reminder letters and a returns page rewritten so that people come back instead of staying away.</p>
          <p class="work-meta">Service design · 2024</p>
          <a class="work-link" href="/work/northfield-library">Read the case study</a>
        </li>
        <li class="work-case">
          <svg class="work-cover" viewBox="0 0 200 150" aria-hidden="true">
            <path d="M 40 52 L 80 42 L 120 52 L 160 42 V 108 L 120 118 L 80 108 L 40 118 Z" />
            <path d="M 80 42 V 108 M 120 52 V 118" />
            <path d="M 52 100 C 70 80 90 96 104 76 S 138 72 148 58" />
          </svg>
          <p class="work-n">04 · Meridian Cycling Club</p>
          <h3 class="work-title">Route maps that fold into a jersey pocket</h3>
          <p class="work-text">Twelve club routes on one sheet, with the climbs drawn to scale and the cafés where they matter.</p>
          <p class="work-meta">Print · 2024</p>
          <a class="work-link" href="/work/meridian-cycling-club">Read the case study</a>
        </li>
      </ol>
    </div>
  </section>

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

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

    const work = document.getElementById('work');
    const stage = work.querySelector('.work-stage');
    const track = work.querySelector('.work-track');
    const count = work.querySelector('.work-count b');
    const fill = work.querySelector('.work-rail span');
    const n = track.children.length;

    // How far the strip travels: its width minus the stage's content box.
    const travel = () => {
      const cs = getComputedStyle(stage);
      const seen = stage.clientWidth - parseFloat(cs.paddingLeft) - parseFloat(cs.paddingRight);
      return Math.max(0, track.scrollWidth - seen);
    };

    // A pixel of scroll moves the strip a pixel: the runway is the strip's
    // travel plus the one screen the stage stays stuck for.
    const fit = () => work.style.setProperty('--travel', `${travel()}px`);
    fit();
    const ro = new ResizeObserver(fit);
    ro.observe(track);
    ro.observe(stage);

    let shown = 0;
    scrollHorizontal(track, {
      onProgress: (p) => {
        fill.style.transform = `scaleX(${p})`;
        const i = Math.round(p * (n - 1));
        if (i !== shown) {
          count.textContent = String(i + 1).padStart(2, '0');
          shown = i;
        }
      },
    });

    // The stage cannot scroll (overflow: clip), so a case reached with Tab is
    // brought into view by scrolling the page to it — on the next frame, after
    // the browser's own scroll to the focused link.
    track.addEventListener('focusin', (e) => {
      const item = e.target.closest('li');
      if (!item) return;
      const t = travel();
      // How far into the track the case starts. Both boxes carry the same
      // translateX, so it cancels out.
      const at = item.getBoundingClientRect().left - track.getBoundingClientRect().left;
      const p = t > 0 ? Math.min(1, at / t) : 0;
      const top = work.getBoundingClientRect().top + scrollY + p * (work.offsetHeight - innerHeight);
      requestAnimationFrame(() => scrollTo({ top }));
    });
  </script>
</body>
</html>
045 problems

Where it goes wrong

01

Passing your own distance

track.scrollWidth - window.innerWidth is the line most tutorials give, and this post gave it too. Measured on this strip at 1280 wide, it stops 154 px early, because the window’s width includes the stage’s padding, and 77 px of the last case never come on screen. Narrow the window to 800 px part-way down and the number is stale: the last case never appeared at all. Leave distance out. Since 2.18.1 the default is measured against the stage and measured again when the layout changes; a number you pass is used as given.

02

The stage scrolls away instead of sticking

html, body { overflow-x: hidden } is a common fix for stray sideways scrolling. With both set, body becomes a scroll container that never scrolls, and the stage sticks to that instead of the screen: measured halfway along this strip, the stage had gone 513 px up off the screen while the strip kept moving inside it. overflow-x: clip stops sideways scrolling without making a scroll container.

03

Reduced motion shows the end first

With respectReducedMotion: true and reduced motion asked for, an animation jumps to its final state, and here the final state is the far end of the strip: measured, the track sat at its end from the first pixel of scroll and the first case was never on screen. The default, false, keeps the scrub, which moves only as the reader scrolls, and every case stays reachable.

04

Reveals inside the strip do not wait for each case

scrollReveal times each element by its height on the page, and every case in the stage is at the same height. Measured with scrollReveal('.work-case') on this strip: the cases on screen as the stage arrived faded in on the way up, and the fourth, off to the right at that point, came on screen already at full opacity. To reveal each case as it slides on, drive it from the strip’s own onProgress.

05

It goes wrong after the page has loaded (2.18.0 and older)

Older versions measured the travel and the runway once, at load. On a test page with 2.18.0, a 480 px hero image that loaded late, above the strip, had it a fifth of the way along before the stage pinned, and narrowing the window from 1280 to 800 px ran it 1,200 px past its last panel. On those versions, call refresh() after anything above or inside the strip changes size; 2.18.1 does it for you.

05triggerElement

Which element sets the runway

The scroll window is measured from the outer container, not the track. The track can’t supply it: the sticky stage pins the track at exactly one viewport tall, so measuring against it would put the start and end of the window at the same scroll position, and nothing would move. scrollHorizontal finds the outer container for you by walking up to the nearest position: sticky ancestor and taking its parent, which is the structure above.

If your layout doesn't use a sticky stage — a custom pinning approach, or a nested scroll container — pass the element that owns the scroll length explicitly:

app.js3 lines
scrollHorizontal(track, {
  triggerElement: '#outer',   // the element whose height is the scroll runway
});
06Reference

API

OptionTypeDescription
distancenumberHorizontal travel in px. Default: the track’s width minus the sticky stage’s content box, measured again whenever the layout changes. Leave it out unless you want a fixed number
easingEasingName | fnEasing for horizontal movement. Default: linear
triggerTriggerConfigScroll window. Default: top top → bottom bottom
scrollContainerstring | ElementCustom scroll container. Default: window
triggerElementstring | ElementElement whose height is the scroll runway. Default: the container of the nearest position:sticky ancestor
respectReducedMotionbooleanDefault: false. The scrub advances only as the user scrolls, and holding a final state would hide every panel but one
onProgress(p: number) => voidProgress 0–1 through the horizontal travel
→Parts list

Keep reading

More from the blog