Sticky pinned section on scroll

Pinning is the pattern behind almost every product walkthrough: an image holds still while copy scrolls past it. Doing it with position: sticky alone breaks down the moment you need to know when the pin started or ended. scrollPin wraps the target in a spacer so nothing shifts, and gives you onEnter, onLeave, onEnterBack and onLeaveBack.

014 steps

Build it, step by step

  1. Step 01 · feature-tour.css · FeatureTour.tsx

    Build a stage that fits one screen

    The stage is what stays put: the drawings and the copy, side by side. While it is pinned it is fixed below your header, so anything taller than the screen can never be scrolled to. Size it to the viewport minus the header, and give CSS the header height from the same constant the script uses.

    feature-tour.css7 lines
    .tour-stage {
      height: calc(100vh - var(--header));
      height: calc(100svh - var(--header));
    }
    
    <div ref={stage} className="tour-stage"
      style={{ '--header': `${HEADER}px` }}>
    Fig. 1.1One screen
    header100svh− header.tour-stage
  2. Step 02 · FeatureTour.tsx

    Pin it for two screens

    scrollPin wraps the stage in a div as tall as the stage plus pinDistance. The stage scrolls in as usual, stays fixed at top while the wrapper scrolls past, then comes to rest at the wrapper’s bottom. Two screens of pinDistance give three features two-thirds of a screen each.

    FeatureTour.tsx7 lines
    const pin = scrollPin(stage.current, {
      top: HEADER,
      pinDistance: `${(n - 1) * 100}vh`,
      onProgress: (p) => { … },
    });
    
    return () => pin.destroy();
    Fig. 1.2before · pinned · after
    beforepinnedafterdashed: wrapper · grey: screen · ink: stage
  3. Step 03 · FeatureTour.tsx

    Let the progress pick the feature

    onProgress reports 0 where the pin starts and 1 where it ends, on every frame in between. Cut it into thirds: floor(p × 3) is the feature to show. The copy and the drawing for that feature come forward; the others step back.

    FeatureTour.tsx4 lines
    onProgress: (p) => {
      const i = Math.min(n - 1, Math.floor(p * n));
      setActive(i);
    }
    Fig. 1.3Progress, in thirds
    0102030⅓⅔1i = floor(p × 3)
  4. Step 04 · FeatureTour.tsx

    Draw each feature in its third

    Give each drawing its own scrollDraw and move it from the pin with seek() instead of its own position on the page: earlier features finished, later ones empty, and the current one at p × 3 − i, how far the reader is through its third.

    FeatureTour.tsx7 lines
    const drawings = Array.from(stage.current.querySelectorAll('.tour-art'),
      (svg) => scrollDraw(svg));
    drawings.forEach((d) => d.seek(0));
    
    // inside onProgress
    drawings.forEach((d, j) =>
      d.seek(j < i ? 1 : j > i ? 0 : p * n - i));

    Under reduced motion each drawing is shown finished and seek() does nothing. The pin and the features keep working: pinning is not motion.

    Fig. 1.4A third each

    01 · p × 3

    02 · p × 3 − 1

    03 · p × 3 − 2

02The whole file

The complete component

Two files, ready to paste into a React or Next.js project with svg-scroll-draw installed. The section below them is this exact component, running: scroll through it.

FeatureTour.tsx98 lines
'use client';

import { useEffect, useRef, useState, type CSSProperties } from 'react';
import { scrollDraw } from 'svg-scroll-draw';
import { scrollPin } from 'svg-scroll-draw/pin';
import './feature-tour.css';

/** The height of your sticky header, so the stage pins just below it. */
const HEADER = 64;

const FEATURES = [
  {
    title: 'From three farms',
    body: 'A washed Yirgacheffe, a honey Tarrazú and a natural from Huila, bought direct from the people who grow them.',
    art: [
      'M 8 112 C 44 84 78 88 104 98 S 160 80 192 92',
      'M 8 138 C 58 116 122 132 192 120',
      'M 30 104 q 7 -11 14 0 q 7 -11 14 0 q 7 -11 14 0 q 7 -11 14 0',
      'M 150 26 A 16 16 0 1 1 150 58 A 16 16 0 1 1 150 26',
    ],
  },
  {
    title: 'Roasted on Tuesday',
    body: 'Small batches in Lisbon every Tuesday, so the bag you open was roasted days ago, not months.',
    art: [
      'M 64 46 H 136 A 32 32 0 0 1 136 110 H 64 A 32 32 0 0 1 64 46 Z',
      'M 100 62 A 16 16 0 1 1 100 94 A 16 16 0 1 1 100 62',
      'M 120 46 V 24 H 132 V 46',
      'M 70 110 V 128 M 130 110 V 128',
      'M 100 152 C 88 142 92 132 100 124 C 108 132 112 142 100 152 Z',
    ],
  },
  {
    title: 'At your door in two days',
    body: 'Packed the morning after roasting and shipped free over €40, in bags you can compost.',
    art: [
      'M 50 62 L 100 42 L 150 62 L 150 118 L 100 138 L 50 118 Z',
      'M 50 62 L 100 82 L 150 62 M 100 82 V 138',
      'M 76 52 L 126 72',
      'M 18 96 H 38 M 24 110 H 40',
    ],
  },
];

export function FeatureTour() {
  const stage = useRef<HTMLDivElement>(null);
  const [active, setActive] = useState(0);

  useEffect(() => {
    const el = stage.current;
    if (!el) return;
    const n = FEATURES.length;

    // One drawing per feature, moved by the pin rather than by its own position.
    const drawings = Array.from(el.querySelectorAll('.tour-art'), (svg) => scrollDraw(svg));
    drawings.forEach((d) => d.seek(0));

    const pin = scrollPin(el, {
      top: HEADER,
      pinDistance: `${(n - 1) * 100}vh`, // three features, two screens of scroll
      onProgress: (p) => {
        const i = Math.min(n - 1, Math.floor(p * n));
        setActive(i);
        drawings.forEach((d, j) => d.seek(j < i ? 1 : j > i ? 0 : p * n - i));
      },
    });

    return () => {
      pin.destroy();
      drawings.forEach((d) => d.destroy());
    };
  }, []);

  return (
    // scrollPin moves .tour-stage into a wrapper of its own, so the stage must
    // not be this component's root: React looks for its root where it left it.
    <section className="tour">
      <div ref={stage} className="tour-stage" style={{ '--header': `${HEADER}px` } as CSSProperties}>
        <div className="tour-art-box">
          {FEATURES.map((f, i) => (
            <svg key={f.title} className="tour-art" data-on={i === active || undefined} viewBox="0 0 200 160" aria-hidden="true">
              {f.art.map((d) => <path key={d} d={d} />)}
            </svg>
          ))}
        </div>
        <ol className="tour-copy">
          {FEATURES.map((f, i) => (
            <li key={f.title} aria-current={i === active ? 'step' : undefined}>
              <span className="tour-n">0{i + 1}</span>
              <h3>{f.title}</h3>
              <p>{f.body}</p>
            </li>
          ))}
        </ol>
      </div>
    </section>
  );
}
feature-tour.css61 lines
.tour {
  --ink: #2a1b12;
  --paper: #f4ede3;
  --accent: #c4572b;
  background: var(--paper);
  color: var(--ink);
}

/* The stage fills one screen below the header: while pinned, whatever does
   not fit can never be scrolled to. */
.tour-stage {
  box-sizing: border-box;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  align-items: center;
  gap: 48px;
  height: calc(100vh - var(--header));
  height: calc(100svh - var(--header));
  padding: 0 6vw;
}

.tour-art-box { position: relative; aspect-ratio: 5 / 4; }
.tour-art {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
  opacity: 0;
  transition: opacity 0.3s ease;
}
.tour-art[data-on] { opacity: 1; }
.tour-art path {
  fill: none;
  stroke: var(--ink);
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.tour-copy { display: grid; gap: 28px; margin: 0; padding: 0; list-style: none; }
/* Not the current feature: greyed by colour, not opacity, so its text keeps
   WCAG AA (at 35% opacity it measured about 2:1). #75695f is 4.6:1 on the paper. */
.tour-copy li { color: #75695f; transition: color 0.3s ease; }
.tour-copy li[aria-current] { color: var(--ink); }
.tour-copy li:not([aria-current]) .tour-n { color: inherit; }
/* The accent, darkened for small text: #c4572b is 3.81:1 on the paper, #ae4d26 4.62:1. */
.tour-n { font: 600 12px/1 ui-monospace, monospace; color: #ae4d26; }
.tour-copy h3 { margin: 8px 0 8px; font: 600 24px/1.2 system-ui, sans-serif; }
.tour-copy p { max-width: 36ch; margin: 0; font: 400 16px/1.6 system-ui, sans-serif; }

/* On a phone the stage still has to fit: show one feature at a time. */
@media (max-width: 720px) {
  .tour-stage { grid-template-columns: minmax(0, 1fr); align-content: center; gap: 24px; }
  .tour-art-box { width: 100%; max-width: 280px; margin: 0 auto; }
  .tour-copy li:not([aria-current]) { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .tour-art, .tour-copy li { transition: none; }
}
Fig. 2.1this file, running — keep scrolling
  1. 01

    From three farms

    A washed Yirgacheffe, a honey Tarrazú and a natural from Huila, bought direct from the people who grow them.

  2. 02

    Roasted on Tuesday

    Small batches in Lisbon every Tuesday, so the bag you open was roasted days ago, not months.

  3. 03

    At your door in two days

    Packed the morning after roasting and shipped free over €40, in bags you can compost.

No build step

The same tour as one HTML file. scrollPin is not in the script-tag build, so this imports the ES modules from a CDN. Save it and open it in a browser.

index.html153 lines
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Pinned feature tour</title>
  <style>
    body { margin: 0; }
    .spacer { height: 100vh; }

    .tour {
      --ink: #2a1b12;
      --paper: #f4ede3;
      --accent: #c4572b;
      --header: 64px;
      background: var(--paper);
      color: var(--ink);
    }

    /* The stage fills one screen below the header: while pinned, whatever does
       not fit can never be scrolled to. */
    .tour-stage {
      box-sizing: border-box;
      display: grid;
      grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
      align-items: center;
      gap: 48px;
      height: calc(100vh - var(--header));
      height: calc(100svh - var(--header));
      padding: 0 6vw;
    }

    .tour-art-box { position: relative; aspect-ratio: 5 / 4; }
    .tour-art {
      position: absolute;
      inset: 0;
      width: 100%;
      height: 100%;
      overflow: visible;
      opacity: 0;
      transition: opacity 0.3s ease;
    }
    .tour-art[data-on] { opacity: 1; }
    .tour-art path {
      fill: none;
      stroke: var(--ink);
      stroke-width: 2;
      stroke-linecap: round;
      stroke-linejoin: round;
    }

    .tour-copy { display: grid; gap: 28px; margin: 0; padding: 0; list-style: none; }
    /* Not the current feature: greyed by colour, not opacity, so it keeps WCAG AA. */
    .tour-copy li { color: #75695f; transition: color 0.3s ease; }
    .tour-copy li[aria-current] { color: var(--ink); }
    .tour-copy li:not([aria-current]) .tour-n { color: inherit; }
    .tour-n { font: 600 12px/1 ui-monospace, monospace; color: #ae4d26; } /* the accent darkened to AA for small text */
    .tour-copy h3 { margin: 8px 0 6px; font: 600 24px/1.2 system-ui, sans-serif; }
    .tour-copy p { max-width: 36ch; margin: 0; font: 400 16px/1.6 system-ui, sans-serif; }

    /* On a phone the stage still has to fit: show one feature at a time. */
    @media (max-width: 720px) {
      .tour-stage { grid-template-columns: minmax(0, 1fr); align-content: center; gap: 24px; }
      .tour-art-box { width: 100%; max-width: 280px; margin: 0 auto; }
      .tour-copy li:not([aria-current]) { display: none; }
    }

    @media (prefers-reduced-motion: reduce) {
      .tour-art, .tour-copy li { transition: none; }
    }
  </style>
</head>
<body>
  <div class="spacer"></div>

  <section class="tour">
    <div class="tour-stage" id="stage">
      <div class="tour-art-box">
        <svg class="tour-art" data-on viewBox="0 0 200 160" aria-hidden="true">
          <path d="M 8 112 C 44 84 78 88 104 98 S 160 80 192 92" />
          <path d="M 8 138 C 58 116 122 132 192 120" />
          <path d="M 30 104 q 7 -11 14 0 q 7 -11 14 0 q 7 -11 14 0 q 7 -11 14 0" />
          <path d="M 150 26 A 16 16 0 1 1 150 58 A 16 16 0 1 1 150 26" />
        </svg>
        <svg class="tour-art" viewBox="0 0 200 160" aria-hidden="true">
          <path d="M 64 46 H 136 A 32 32 0 0 1 136 110 H 64 A 32 32 0 0 1 64 46 Z" />
          <path d="M 100 62 A 16 16 0 1 1 100 94 A 16 16 0 1 1 100 62" />
          <path d="M 120 46 V 24 H 132 V 46" />
          <path d="M 70 110 V 128 M 130 110 V 128" />
          <path d="M 100 152 C 88 142 92 132 100 124 C 108 132 112 142 100 152 Z" />
        </svg>
        <svg class="tour-art" viewBox="0 0 200 160" aria-hidden="true">
          <path d="M 50 62 L 100 42 L 150 62 L 150 118 L 100 138 L 50 118 Z" />
          <path d="M 50 62 L 100 82 L 150 62 M 100 82 V 138" />
          <path d="M 76 52 L 126 72" />
          <path d="M 18 96 H 38 M 24 110 H 40" />
        </svg>
      </div>
      <ol class="tour-copy">
        <li aria-current="step">
          <span class="tour-n">01</span>
          <h3>From three farms</h3>
          <p>A washed Yirgacheffe, a honey Tarrazú and a natural from Huila, bought direct from the people who grow them.</p>
        </li>
        <li>
          <span class="tour-n">02</span>
          <h3>Roasted on Tuesday</h3>
          <p>Small batches in Lisbon every Tuesday, so the bag you open was roasted days ago, not months.</p>
        </li>
        <li>
          <span class="tour-n">03</span>
          <h3>At your door in two days</h3>
          <p>Packed the morning after roasting and shipped free over €40, in bags you can compost.</p>
        </li>
      </ol>
    </div>
  </section>

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

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

    const HEADER = 64; // the height of your sticky header
    const stage = document.getElementById('stage');
    const arts = [...stage.querySelectorAll('.tour-art')];
    const items = [...stage.querySelectorAll('.tour-copy li')];
    const n = items.length;

    // One drawing per feature, moved by the pin rather than by its own position.
    const drawings = arts.map((svg) => scrollDraw(svg));
    drawings.forEach((d) => d.seek(0));

    let shown = 0;
    scrollPin(stage, {
      top: HEADER,
      pinDistance: `${(n - 1) * 100}vh`, // three features, two screens of scroll
      onProgress: (p) => {
        const i = Math.min(n - 1, Math.floor(p * n));
        if (i !== shown) {
          arts[shown].removeAttribute('data-on');
          items[shown].removeAttribute('aria-current');
          arts[i].setAttribute('data-on', '');
          items[i].setAttribute('aria-current', 'step');
          shown = i;
        }
        drawings.forEach((d, j) => d.seek(j < i ? 1 : j > i ? 0 : p * n - i));
      },
    });
  </script>
</body>
</html>
035 problems

Where it goes wrong

01

Part of the stage is cut off on a phone

A pinned stage taller than the screen keeps its bottom out of reach for as long as it is pinned. Keep it to 100svh minus the header, and on a narrow screen show one feature at a time, as the CSS here does below 720px.

02

It drifts or scrolls away while pinned

While pinned the stage is position: fixed, and an ancestor with a transform, filter, perspective or contain: paint turns fixed into “fixed to me”. Move the tour out of that ancestor, or drop the property from it.

03

The layout changes the moment it pins

scrollPin moves the stage into a wrapper div. A selector like .page > .tour-stage stops matching, and in a grid or flex parent the wrapper becomes the item. Style the stage by its own class.

04

React: “The node to be removed is not a child of this node”

The pinned element is the component’s root. React removes its root from where it put it, the stage has moved into the wrapper, and the whole app goes blank on unmount. Pin an element inside your root, as <section className="tour"> does here.

05

The tour is the wrong length after the phone rotates

pinDistance: window.innerHeight * 2 is a number, measured once, so it keeps the old screen’s height. Write '200vh' instead (2.20.0): it is measured as CSS measures vh, so a rotation re-measures it and the address bar sliding in and out does not.

23scrollPin · onProgress · onEnter/onLeave

In a real site

A compact camera's product page. The camera, drawn in line, stays pinned while a scroll-driven tour moves a focus frame across its sensor, lens, stabiliser and battery, x-rays each part and scrubs the camera's own readout.

Lumen, a compact camera’s product page: the first screen of its demo site

A compact camera’s product page

Lumen

Live site

Related examples

Make your own in the scroll pin 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 →