Skip to content

176 web animation techniques, each a live demo with plain settings and a ready-made prompt for your AI assistant. No dependencies, no build step.

24.9 KB
Raw Blame
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Scrollytelling — Animation Handbook</title>
  <meta name="description" content="A picture beside the story changes as you read down. Best for data stories.">
  <link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/01-scroll-based/scrollytelling/">
  <link rel="icon" type="image/png" sizes="96x96" href="../../../favicon.png">
  <link rel="icon" type="image/svg+xml" href="../../../favicon.svg">
  <link rel="apple-touch-icon" href="../../../apple-touch-icon.png">
  <meta property="og:type" content="article">
  <meta property="og:site_name" content="Animation Handbook">
  <meta property="og:title" content="Scrollytelling — Animation Handbook">
  <meta property="og:description" content="A picture beside the story changes as you read down. Best for data stories.">
  <meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/01-scroll-based/scrollytelling/">
  <meta property="og:image" content="https://matinmonshizadeh.github.io/animation-handbook/og-image.png">
  <meta name="twitter:card" content="summary_large_image">
  <meta name="twitter:title" content="Scrollytelling — Animation Handbook">
  <meta name="twitter:description" content="A picture beside the story changes as you read down. Best for data stories.">
  <meta name="twitter:image" content="https://matinmonshizadeh.github.io/animation-handbook/og-image.png">
  <script type="application/ld+json">{"@context": "https://schema.org", "@type": "TechArticle", "headline": "Scrollytelling", "name": "Scrollytelling", "description": "A picture beside the story changes as you read down. Best for data stories.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/01-scroll-based/scrollytelling/", "inLanguage": "en", "articleSection": "Scroll-Based", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, Scroll-Based, Scrollytelling"}</script>
  <style>
    :root{--ui-accent:#6ea8ff;--ui-border:rgba(255,255,255,.09);--ui-muted:#8a8a92}
    *,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
    .stage{position:relative;overflow-y:auto;scrollbar-width:none;container-type:size;background:#0b0b0d;display:flex}
    .stage::-webkit-scrollbar{display:none}
    .vis{position:sticky;top:0;width:45%;height:100cqh;flex-shrink:0;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:20px;padding:28px;overflow:hidden}
    .chapters{flex:1;padding:20px 28px}
    .ch{min-height:90cqh;display:flex;flex-direction:column;justify-content:center;padding:40px 0;border-bottom:1px solid var(--ui-border)}
    .ch:last-child{border-bottom:none}
    .ch-eye{font-size:11px;text-transform:uppercase;letter-spacing:2px;margin-bottom:8px}
    .ch-head{font-size:clamp(18px,2.6vw,26px);font-weight:bold;line-height:1.2;margin-bottom:14px}
    .ch-body{font-size:14px;line-height:1.7;opacity:.65;max-width:300px;margin-bottom:12px}
    .ch-fact{border-left:3px solid var(--ui-accent);padding-left:12px;font-size:14px;opacity:.5;line-height:1.6}
    /* The porthole shrinks in short boxes so it and the depth under it fit the column */
    #porthole,.cp-ph{width:min(220px,85%,calc(100cqh - 106px));aspect-ratio:1;border-radius:50%;border:4px solid #00c8ff;overflow:hidden;flex-shrink:0;position:relative}
    #ph-bg,.cp-bg{position:absolute;inset:0}
    #ph-shimmer,.cp-sh{position:absolute;inset:0;background:linear-gradient(145deg,rgba(255,255,255,.06) 0%,transparent 50%)}
    #depth-num,.cp-dn{font-size:clamp(26px,3.5vw,42px);font-weight:bold;letter-spacing:2px;font-variant-numeric:tabular-nums;text-align:center}
    #depth-lbl,.cp-dl{font-size:11px;color:var(--ui-muted);letter-spacing:3px;text-transform:uppercase;text-align:center}

    /* See inside and Compare. The picture and its copy sit in .pic (it adds no box in Normal); the reading line and the
       bars are drawn on the chapters, the blend bar under the depth, and none of them changes a size or a place in the flow. */
    .fx,.bar,.bl,.cp,.pic-n,.cm-div,.bl-b[hidden]{display:none}
    .pic{display:contents}
    .cap{padding:2px 6px;border-radius:999px;white-space:nowrap;background:rgba(11,11,13,.95);color:#f4f4f2;border:1px solid rgba(244,244,242,.3);font:600 12.5px/16px var(--hb-font)}
    .stage[data-hb-mode="inside"],.stage[data-hb-mode="compare"]{overflow-anchor:none}
    /* its place in the flow is below the chapters' top padding: the margins lift it to the top of the scroll and give the space back */
    .fx{position:sticky;top:0;height:0;z-index:3;pointer-events:none;margin:calc(-1 * var(--fxo,0px)) 0 var(--fxo,0px)}
    .fx-line{position:absolute;left:-14px;right:-28px;top:calc(42cqh - 1px);height:2px;background:#ffd166;box-shadow:0 0 0 1px #000}
    .fx-cap{position:absolute;right:0;top:calc(42cqh + 5px)}
    /* the chapters' box is as tall as their text, so the layer in it keeps sticking to the top of the box all the way down */
    @media(min-width:601px){
      .stage[data-hb-mode="inside"] .chapters{align-self:flex-start;padding-bottom:0}
    }
    .stage[data-hb-mode="inside"] .ch:last-child .bar{bottom:0}
    .stage[data-hb-mode="inside"] .fx{display:flow-root}
    .stage[data-hb-mode="inside"] .ch{position:relative}
    .stage[data-hb-mode="inside"] .ch-eye,.stage[data-hb-mode="inside"] .ch-head{opacity:.6}
    .stage[data-hb-mode="inside"] .ch-body{opacity:.4}
    .stage[data-hb-mode="inside"] .ch-fact{opacity:.32}
    .stage[data-hb-mode="inside"] .bar{display:block;position:absolute;left:-14px;top:0;bottom:-1px;width:4px;border-radius:2px;background:rgba(244,244,242,.2);box-shadow:0 0 0 1px rgba(0,0,0,.7)}
    .stage[data-hb-mode="inside"] .ch0 .bar{top:var(--b0,0px)}
    .bar i{position:absolute;inset:0;border-radius:2px;background:#ffd166;transform:scaleY(0);transform-origin:top}
    .stage[data-hb-mode="inside"] .vis{gap:10px;padding-top:12px;padding-bottom:12px}
    .stage[data-hb-mode="inside"] #porthole{width:min(150px,85%,calc(100cqh - 180px))}
    .stage[data-hb-mode="inside"] .bl{display:block;width:min(200px,calc(45cqw - 60px));margin:6px auto 0}
    .bl-trk{position:relative;height:8px;border-radius:4px;border:1px solid rgba(244,244,242,.7)}
    .bl-dot{position:absolute;left:0;top:50%;width:14px;height:14px;margin:-7px 0 0 -7px;border-radius:50%;border:2px solid #f4f4f2;box-shadow:0 0 0 1px #000}
    .bl-lab{display:flex;justify-content:space-between;align-items:center;margin-top:7px}
    .bl-lab .cap{padding:2px 4px;font-size:12px}
    /* Compare: the picture twice in the same column (or strip), side by side; the left copy shows only the chapter's own values */
    .cp{display:none}
    .stage[data-hb-mode="compare"] .vis{flex-direction:row;align-items:stretch;gap:0;padding:0 6px}
    .stage[data-hb-mode="compare"] .pic,.stage[data-hb-mode="compare"] .cp{display:flex;flex-direction:column;align-items:center;justify-content:center;gap:10px;flex:1 1 0;min-width:0;position:relative;padding:34px 0 10px}
    .stage[data-hb-mode="compare"] .pic-n{display:block;position:absolute;top:8px;left:50%;transform:translateX(-50%)}
    .stage[data-hb-mode="compare"] .cm-div{display:block;position:absolute;left:50%;top:10px;bottom:10px;width:2px;margin-left:-1px;background:#f4f4f2}
    .stage[data-hb-mode="compare"] #porthole,.stage[data-hb-mode="compare"] .cp-ph{width:min(150px,92%,calc(100cqh - 150px))}
    .stage[data-hb-mode="compare"] #depth-num,.stage[data-hb-mode="compare"] .cp-dn{font-size:clamp(20px,2.4vw,30px);letter-spacing:1px}
    /* Phones: the porthole and the depth become a strip across the top and the chapters scroll under it */
    @media(max-width:600px){
      .stage{flex-direction:column}
      .vis{position:sticky;top:0;width:100%;height:112px;flex-direction:row;gap:16px;padding:14px 16px;z-index:2;background:#0b0b0d}
      #porthole,.cp-ph{width:84px;border-width:3px}
      .chapters{padding:12px 16px}
      .fx-line{left:-12px;right:-16px}
      .stage[data-hb-mode="inside"] .bar{left:-12px}
      .stage[data-hb-mode="inside"] .vis{padding:6px 16px;gap:12px}
      .stage[data-hb-mode="inside"] #porthole{width:56px}
      .stage[data-hb-mode="inside"] .bl{width:140px;margin-top:4px}
      .stage[data-hb-mode="compare"] .vis{padding:0 8px}
      .stage[data-hb-mode="compare"] .pic,.stage[data-hb-mode="compare"] .cp{flex-direction:row;gap:8px;padding:28px 0 4px}
      .stage[data-hb-mode="compare"] .pic-n{top:5px}
      .stage[data-hb-mode="compare"] #porthole,.stage[data-hb-mode="compare"] .cp-ph{width:60px}
      .stage[data-hb-mode="compare"] #depth-lbl,.stage[data-hb-mode="compare"] .cp-dl{display:none}
      .stage[data-hb-mode="compare"] #depth-num,.stage[data-hb-mode="compare"] .cp-dn{font-size:20px;letter-spacing:0}
    }
    @media(max-width:360px){
      .stage[data-hb-mode="inside"] .bl{width:128px}
      .stage[data-hb-mode="compare"] .vis{padding:0 2px}
      .stage[data-hb-mode="compare"] .pic-n{font-size:12px;padding:2px 3px}
      .stage[data-hb-mode="compare"] .pic,.stage[data-hb-mode="compare"] .cp{gap:6px}
      .stage[data-hb-mode="compare"] #porthole,.stage[data-hb-mode="compare"] .cp-ph{width:46px;border-width:2px}
      .stage[data-hb-mode="compare"] #depth-num,.stage[data-hb-mode="compare"] .cp-dn{font-size:17px}
    }
    .pic-n span{display:inline-block;line-height:16px}
    @media(min-width:701px) and (max-width:800px){
      .stage[data-hb-mode="compare"] .vis{padding:0 2px}
      .stage[data-hb-mode="compare"] .pic-n{font-size:12px;padding:2px 4px}
    }
    /* Just over the phone width the box gives each half under 140px: both half names then take two lines, so they keep a gap on both sides */
    @media(min-width:601px) and (max-width:700px){
      .stage[data-hb-mode="compare"] .pic,.stage[data-hb-mode="compare"] .cp{padding-top:46px}
      .stage[data-hb-mode="compare"] .pic-n{font-size:12px;line-height:16px;padding:2px 8px;border-radius:12px;text-align:center}
      .stage[data-hb-mode="compare"] .pic-n span{display:block}
    }
  </style>
  <link rel="stylesheet" href="../../../assets/css/demo-page.css?v=11">
  <script src="../../../assets/js/demo-page.js?v=11" defer></script>
</head>
<body class="hb" data-hb-kind="scroll" data-hb-autoplay>
<nav class="hb-bar" aria-label="Animation Handbook">
  <a class="hb-home" href="../../../"><svg class="hb-mark" viewBox="0 0 32 32" aria-hidden="true"><g class="g"><circle cx="11.16" cy="20.84" r="6.1"/><circle cx="15.12" cy="16.88" r="6.1"/><circle cx="18.51" cy="13.49" r="6.1"/></g><circle class="b" cx="21.2" cy="10.8" r="6.6"/></svg><span>Animation Handbook</span></a>
  <span class="hb-pager"><a href="../snap-scrolling/" rel="prev" aria-label="Previous: Snap Scrolling"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m15 18-6-6 6-6"/></svg><span><span class="hb-dir">Previous: </span>Snap Scrolling</span></a><a href="../reveal-on-scroll/" rel="next" aria-label="Next: Reveal on Scroll"><span><span class="hb-dir">Next: </span>Reveal on Scroll</span><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m9 18 6-6-6-6"/></svg></a></span>
  <a class="hb-star" href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener" aria-label="Star on GitHub"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3l2.7 5.6 6.1.9-4.4 4.3 1 6.1L12 17l-5.4 2.9 1-6.1-4.4-4.3 6.1-.9z"/></svg><span>Star on GitHub</span></a>
</nav>
<main class="hb-page">
  <header class="hb-head">
    <p class="hb-cat">01.11 · Scroll-Based</p>
    <h1>Scrollytelling</h1>
    <p class="hb-lede">A picture beside the story changes as you read down. Best for data stories.</p>
  </header>

  <section class="hb-step hb-watch" aria-labelledby="hb-watch-title">
    <div class="hb-step-head">
      <span class="hb-num" aria-hidden="true">1</span>
      <div><h2 id="hb-watch-title">Scroll it</h2><p>Scroll inside the box, or press Play and it scrolls for you.</p></div>
    </div>
    <div class="seg hb-modes" role="group" aria-label="What the box shows" data-hb-modes>
      <button type="button" data-hb-mode="normal" class="on" aria-pressed="true">Normal</button>
      <button type="button" data-hb-mode="inside" aria-pressed="false">See inside</button>
      <button type="button" data-hb-mode="compare" aria-pressed="false">Compare</button>
    </div>
    <div class="stage" id="stage" tabindex="0" role="region" aria-label="Six chapters about the ocean's depths">
      <div class="vis">
        <i class="cm-div" aria-hidden="true"></i>
        <div class="pic cp" aria-hidden="true" inert>
          <span class="cap pic-n" data-hb-caption><span>Jumps each</span> <span>chapter</span></span>
          <div class="cp-ph"><div class="cp-bg"></div><div class="cp-sh"></div></div>
          <div><div class="cp-dn">0</div><div class="cp-dl">meters depth</div></div>
        </div>
        <div class="pic">
          <span class="cap pic-n" data-hb-caption aria-hidden="true"><span>Blends</span> <span>smoothly</span></span>
          <div id="porthole" aria-hidden="true"><div id="ph-bg"></div><div id="ph-shimmer"></div></div>
          <div><div id="depth-num" role="img" aria-label="0 meters deep">0</div><div id="depth-lbl" aria-hidden="true">meters depth</div>
            <div class="bl" aria-hidden="true"><div class="bl-trk"><i class="bl-dot"></i></div><div class="bl-lab"><span class="cap bl-a" data-hb-caption></span><span class="cap bl-b" data-hb-caption></span></div></div></div>
        </div>
      </div>
      <div class="chapters" id="chapters"></div>
    </div>
    <div class="hb-player">
      <button class="hb-play" type="button" id="btn-scroll" data-hb-autoscroll><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 5v14M19 12l-7 7-7-7"/></svg>Play</button>
      <button class="hb-play" type="button" id="btn-top" data-hb-top><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 19V5M5 12l7-7 7 7"/></svg>Back to top</button>
    </div>
    <div class="hb-mode-notes" aria-live="polite">
      <p data-hb-for="inside" hidden>Inside, each chapter's bar fills as the reading line passes through it, and the picture blends toward the next chapter's depth and color by that share. Scroll the box or press Play to watch.</p>
      <p data-hb-for="compare" hidden>Jumps each chapter changes the picture only when a new chapter crosses the reading line; Blends smoothly moves it a little with every bit you scroll. Scroll the box or press Play to watch.</p>
    </div>
  </section>

  <section class="hb-step hb-prompt-step" aria-labelledby="hb-prompt-title">
    <div class="hb-step-head">
      <span class="hb-num" aria-hidden="true">2</span>
      <div><h2 id="hb-prompt-title">Copy the prompt</h2><p>Paste it into an AI assistant such as ChatGPT, Claude or Cursor to build this in your own project.</p></div>
    </div>
    <div class="hb-card">
      <p class="hb-prompt">Add a scrollytelling layout to [your story and the picture that goes with it]. Put the story in a column of chapters that scrolls, and keep the picture in view beside it. Work out which chapter has passed a reading line about 40% down the view and how far it is toward the next, as a fraction, and blend every value in the picture between the two chapters with it, such as colors and a counting number, so the picture changes smoothly as the visitor reads. On phones, pin a short strip of the picture above the text. If the visitor has reduced motion turned on, change the picture without fading.</p>
      <div class="hb-chips-box" hidden>
        <p class="hb-chips-title">Your settings</p>
        <ul class="hb-chips"></ul>
      </div>
      <div class="hb-copy-row">
        <button class="hb-copy" type="button"><svg class="hb-ic hb-i-copy" viewBox="0 0 24 24" aria-hidden="true"><rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg><svg class="hb-ic hb-i-done" viewBox="0 0 24 24" aria-hidden="true"><path d="M20 6 9 17l-5-5"/></svg><span class="hb-copy-label" aria-live="polite">Copy prompt</span></button>
        <p class="hb-hint">Replace the words in brackets with your own story and picture.</p>
      </div>
    </div>
    <div class="hb-star-row"><p>Free and open source. If this helped, a star on GitHub helps other people find it.</p><a class="hb-star-btn" href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3l2.7 5.6 6.1.9-4.4 4.3 1 6.1L12 17l-5.4 2.9 1-6.1-4.4-4.3 6.1-.9z"/></svg>Star on GitHub</a></div>
  </section>

  <section class="hb-about" aria-labelledby="hb-what-title">
    <div class="hb-what">
      <h2 id="hb-what-title">What it is</h2>
      <div class="hb-what-text"><p>The full explanation is in <a href="README.md">README.md</a>.</p></div>
    </div>
    <div class="hb-fit">
      <h2>Good for</h2>
      <ul class="hb-tags hb-good"><li>Data stories</li><li>Explainers</li><li>Long reads</li></ul>
      <h3>Avoid on</h3>
      <ul class="hb-tags hb-avoid"><li>Short pages</li><li>Quick reference</li></ul>
    </div>
  </section>

  <section class="hb-related" aria-labelledby="hb-related-title" hidden>
    <h2 id="hb-related-title">Similar animations</h2>
    <div class="hb-rel-list"></div>
  </section>
</main>
<footer class="hb-foot"><span>Animation Handbook</span><a href="https://github.com/matinmonshizadeh/animation-handbook" target="_blank" rel="noopener">Source on GitHub</a><a href="#top">Back to top ↑</a></footer>
<script>
  // The switch above the box: the page draws the mode at once, from the same numbers the scroll uses
  let mode='normal',inCi=-1,cpCi=-1,blW=0;
  const LABELS={
    normal:"Six chapters about the ocean's depths",
    inside:"Six chapters about the ocean's depths, with the reading line and how far the picture has blended toward the next chapter",
    compare:'The ocean picture twice beside one story: jumping at each chapter, and blending smoothly'
  };
  document.addEventListener('hb:mode',e=>{
    mode=e.detail.mode;
    stage.setAttribute('aria-label',LABELS[mode]||LABELS.normal);
    inCi=cpCi=-1;fills.length=0;
    remeasure();
  });

  const CHS=[
    {depth:0,    head:'The Surface',       eye:'01 · 0 m',         bg:'#1a4a8c',ac:'#40d8ff',
     body:'Sunlight refracts through wave patterns, scattering blue and gold. This threshold separates two entirely different worlds.',
     fact:'Light penetrates here at full intensity — photosynthesis sustains nearly all ocean life above this line.'},
    {depth:200,  head:'The Sunlit Zone',    eye:'02 · 0–200 m',     bg:'#0d2e5c',ac:'#00aadd',
     body:'The euphotic zone receives enough light for photosynthesis. Plankton, sardines, and sea turtles move through filtered, shifting blue.',
     fact:'95% of all commercial fish species live in this zone. Below this depth, no plant can survive.'},
    {depth:1000, head:'The Twilight Zone',  eye:'03 · 200–1000 m',  bg:'#050e20',ac:'#006688',
     body:'Bioluminescent flashes replace sunlight. Creatures have evolved enormous eyes. Pressure climbs to 100 atmospheres.',
     fact:'The mesopelagic zone holds more fish biomass than all surface fisheries combined — almost entirely uncounted.'},
    {depth:4000, head:'The Midnight Zone',  eye:'04 · 1000–4000 m', bg:'#020810',ac:'#003d55',
     body:'Total darkness. 2–4°C. Marine snow drifts down from the sunlit world above — the only food source for creatures that have never seen light.',
     fact:'This zone covers more Earth surface area than any other habitat, and remains largely unexplored.'},
    {depth:6000, head:'The Abyssal Plain',  eye:'05 · 4000–6000 m', bg:'#010508',ac:'#001f30',
     body:'Sediment plains stretch thousands of kilometers. Pressure exceeds 600 atmospheres. Sea cucumbers and snailfish move through crushing cold.',
     fact:'Manganese nodules blanket the abyss — each the size of a potato, formed over millions of years.'},
    {depth:10935,head:'The Hadal Zone',     eye:'06 · 6000–11000 m',bg:'#010204',ac:'#000e18',
     body:'Challenger Deep. Three vessels have ever visited. More humans have walked on the Moon. The snailfish survives here — the deepest vertebrate on Earth.',
     fact:'1,100 atmospheres of pressure. Equivalent to 50 jumbo jets stacked on your thumbnail.'},
  ];
  const stage=document.getElementById('stage'),chs=document.getElementById('chapters');
  const phBg=document.getElementById('ph-bg'),porthole=document.getElementById('porthole');
  const depNum=document.getElementById('depth-num');
  let ticking=false;
  const fills=[];
  const $=q=>stage.querySelector(q);
  const blTrk=$('.bl-trk'),blDot=$('.bl-dot'),blA=$('.bl-a'),blB=$('.bl-b');
  const cpNum=$('.cp-dn'),cpBg=$('.cp-bg'),cpPh=$('.cp-ph');

  function lerpCol(h1,h2,t){
    const p=s=>[parseInt(s.slice(1,3),16),parseInt(s.slice(3,5),16),parseInt(s.slice(5,7),16)];
    const a=p(h1),b=p(h2);
    return`#${[0,1,2].map(i=>Math.round(a[i]+(b[i]-a[i])*t).toString(16).padStart(2,'0')).join('')}`;
  }
  const clamp=(v,a,b)=>Math.min(Math.max(v,a),b),lerp=(a,b,t)=>a+(b-a)*t;

  // The three deepest colours are nearly invisible on the dark stage, so a label uses its chapter colour mixed half and half with the text colour
  chs.innerHTML='<div class="fx" aria-hidden="true"><i class="fx-line"></i><span class="cap fx-cap" data-hb-caption>Reading line</span></div>';
  const els=[],bars=[];
  CHS.forEach((c,i)=>{
    const d=document.createElement('div');d.className=i?'ch':'ch ch0';
    d.innerHTML=`<div class="ch-eye" style="color:${lerpCol(c.ac,'#f4f4f2',0.5)}">${c.eye}</div>
      <div class="ch-head">${c.head}</div><div class="ch-body">${c.body}</div>
      <div class="ch-fact">${c.fact}</div><i class="bar"><i></i></i>`;
    chs.appendChild(d);els.push(d);bars.push(d.querySelector('.bar i'));
  });

  function update(){
    const N=els.length,st=stage.scrollTop,line=stage.clientHeight*0.42;
    // Where the scrolled content starts, measured from the box's own edge inside its border
    const origin=stage.getBoundingClientRect().top+stage.clientTop-st;
    // The scroll position at which each chapter's top reaches the reading line; the first chapter is current from the start
    const reach=els.map((el,i)=>i?el.getBoundingClientRect().top-origin-line:0);
    let cur=N-1;while(cur>0&&st<reach[cur])cur--;
    // The last chapter to cross the line, and how far the line has traveled from its top toward the next chapter's top
    let frac=cur<N-1?cur+(st-reach[cur])/(reach[cur+1]-reach[cur]):cur;
    frac=clamp(frac,0,N-0.001);
    const ci=Math.floor(frac),cf=frac-ci;
    const c=CHS[ci],cn=CHS[Math.min(ci+1,N-1)];
    const col=lerpCol(c.ac,cn.ac,cf);
    depNum.textContent=Math.round(lerp(c.depth,cn.depth,cf)).toLocaleString();
    depNum.setAttribute('aria-label',depNum.textContent+(depNum.textContent==='1'?' meter deep':' meters deep'));
    depNum.style.color=lerpCol(col,'#f4f4f2',0.5);
    phBg.style.background=`radial-gradient(circle at 50% 40%,${lerpCol(c.bg,cn.bg,cf)},#000 80%)`;
    porthole.style.borderColor=col;
    if(mode==='inside')drawInside(frac,ci,cf,c,cn,col);
    else if(mode==='compare')drawCopy(c);
    ticking=false;
  }

  // See inside: each chapter's bar is full up to the share of the way the reading line has gone through it (the last has no next chapter, so it is full once reached)
  function drawInside(frac,ci,cf,c,cn,col){
    const N=els.length;
    els.forEach((_,i)=>{
      const v=i<N-1?clamp(frac-i,0,1):(frac>=N-1?1:0);
      if(v!==fills[i]){fills[i]=v;bars[i].style.transform=`scaleY(${v})`}
    });
    if(ci!==inCi){
      inCi=ci;
      blTrk.style.background=`linear-gradient(90deg,${c.ac},${cn.ac})`;
      blA.textContent=c.depth.toLocaleString()+' m';
      blB.textContent=cn.depth.toLocaleString()+' m';
      blB.hidden=ci===N-1;
    }
    blDot.style.background=col;
    blDot.style.transform=`translateX(${cf*blW}px)`;
  }
  // Compare: the copy shows only the current chapter's own values, so it changes when a new chapter crosses the line
  function drawCopy(c){
    if(c===CHS[cpCi])return;
    cpCi=CHS.indexOf(c);
    cpNum.textContent=c.depth.toLocaleString();
    cpNum.style.color=lerpCol(c.ac,'#f4f4f2',0.5);
    cpBg.style.background=`radial-gradient(circle at 50% 40%,${c.bg},#000 80%)`;
    cpPh.style.borderColor=c.ac;
  }
  // The first bar starts where the reading line is at the top of the box, as the page's own count does
  function remeasure(){
    stage.style.setProperty('--b0',Math.max(0,stage.clientHeight*0.42-els[0].offsetTop)+'px');
    stage.style.setProperty('--fxo',chs.offsetTop+parseFloat(getComputedStyle(chs).paddingTop)+'px');
    blW=blTrk.clientWidth;
    update();
  }
  function request(){if(!ticking){ticking=true;requestAnimationFrame(update)}}

  stage.addEventListener('scroll',request,{passive:true});
  addEventListener('resize',remeasure);
  remeasure();
  // The chapters' heights depend on the site font
  if(document.fonts&&document.fonts.ready)document.fonts.ready.then(remeasure);
</script>
</body>
</html>