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.

29.7 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>Reverse-Scrolling Columns — Animation Handbook</title>
  <meta name="description" content="Side columns run the opposite way to the middle one. Best for portfolios.">
  <link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/01-scroll-based/reverse-scrolling-columns/">
  <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="Reverse-Scrolling Columns — Animation Handbook">
  <meta property="og:description" content="Side columns run the opposite way to the middle one. Best for portfolios.">
  <meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/01-scroll-based/reverse-scrolling-columns/">
  <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="Reverse-Scrolling Columns — Animation Handbook">
  <meta name="twitter:description" content="Side columns run the opposite way to the middle one. Best for portfolios.">
  <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": "Reverse-Scrolling Columns", "name": "Reverse-Scrolling Columns", "description": "Side columns run the opposite way to the middle one. Best for portfolios.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/01-scroll-based/reverse-scrolling-columns/", "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, Reverse-Scrolling Columns"}</script>
  <style>
    :root{--ui-accent:#6ea8ff;--ui-text:#f4f4f2;--ui-muted:#8a8a92;--ui-border:rgba(255,255,255,.09);--card-bg:#111114}
    *,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
    .stage{position:relative;overflow-y:auto;scrollbar-width:none;container-type:size;background:#0b0b0d}
    .stage::-webkit-scrollbar{display:none}
    .scene{height:320cqh}
    .columns{position:sticky;top:0;height:100cqh;display:flex;gap:8px;overflow:hidden;padding:8px}
    .col{flex:1;overflow:hidden}
    .col-inner{display:flex;flex-direction:column;gap:8px;will-change:transform}
    .card-set{display:flex;flex-direction:column;gap:8px}

    .card{
      background:var(--card-bg);border:1px solid var(--ui-border);
      border-radius:6px;padding:10px 12px;
      display:flex;align-items:center;gap:10px;flex-shrink:0;
    }
    .icon-box{
      width:28px;height:28px;border-radius:4px;flex-shrink:0;
      display:flex;align-items:center;justify-content:center;
    }
    .card-label{font-size:13px;color:var(--ui-text);letter-spacing:.3px}
    .card-meta{font-size:12px;color:var(--ui-muted);margin-top:2px}

    .half{display:contents}
    .col{position:relative}
    .half.twin,.zoom,.carrow,.hname{display:none}
    /* See inside: each column zoomed out so its two copies of the cards fit the box's height and stand still, with a window as big
       as the box would be at that scale. The script slides the window and jumps it a copy; windows and start outlines move by
       transform only, and labels are drawn at their own size. */
    .stage[data-hb-mode="inside"] .columns{gap:0;justify-content:safe center}
    .stage[data-hb-mode="inside"] .col{flex:none;width:calc(var(--zsw) + 18px);overflow:visible}
    .stage[data-hb-mode="inside"] .col-mid{margin:0 88px 0 124px}
    .stage[data-hb-mode="inside"] .col-inner{width:var(--zcw);transform-origin:0 0;will-change:auto}
    .stage[data-hb-mode="inside"] .zoom{display:block;position:absolute;inset:0;pointer-events:none}
    .zoom i,.zoom .zl{position:absolute}
    .zc{left:0;top:0;width:var(--zsw);border:1px solid rgba(244,244,242,.6);border-radius:2px}
    .zs,.zw{left:-3px;top:0;width:calc(var(--zsw) + 6px);border-radius:3px}
    .zs{border:2px dashed rgba(244,244,242,.45)}
    .zw{border:2px solid var(--ui-accent);background:rgba(110,168,255,.2)}
    .zj{left:-7px;width:calc(var(--zsw) + 14px);height:2px;background:#f4f4f2}
    .zoom .ar{left:calc(var(--zsw) + 8px);color:var(--ui-accent)}
    .ar{position:absolute;width:10px;color:#aeaeb6}
    .ar::before{content:"";position:absolute;left:3.5px;width:3px;top:0;bottom:0;border-radius:2px;background:currentColor}
    .ar::after{content:"";position:absolute;left:0;top:0;border:0 solid transparent;border-width:0 5px 8px;border-bottom-color:currentColor}
    .ar.dn::after{top:auto;bottom:0;border-width:8px 5px 0;border-bottom-color:transparent;border-top-color:currentColor}
    .zl,.hname{padding:2px 6px;border-radius:4px;background:rgba(11,11,13,.92);border:1px solid rgba(244,244,242,.28);color:#f4f4f2;
      font:600 12px/1.25 var(--hb-font);white-space:nowrap}
    .zl1,.zl2,.zlj{right:100%;margin-right:10px;transform:translateY(-50%)}
    .zlb{left:calc(100% + 24px);top:50%;transform:translateY(-50%)}
    .zl br{display:none}
    /* Compare: the columns twice, side by side: a copy where every column moves up, and the page itself. An arrow tops each column. */
    .stage[data-hb-mode="compare"]{overflow-anchor:none}
    .stage[data-hb-mode="compare"] .columns{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));grid-template-rows:minmax(0,1fr);gap:0}
    .stage[data-hb-mode="compare"] .half{display:flex;gap:8px;min-width:0;position:relative;padding:30px 8px 0}
    .stage[data-hb-mode="compare"] .half:not(.twin)::before{content:"";position:absolute;left:-1px;top:0;bottom:0;width:2px;background:rgba(244,244,242,.22)}
    .stage[data-hb-mode="compare"] .col{container-type:inline-size;overflow:visible clip}
    .stage[data-hb-mode="compare"] .card-label{white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
    .stage[data-hb-mode="compare"] .card>div:last-child{min-width:0}
    .stage[data-hb-mode="compare"] .carrow{display:flex;position:absolute;z-index:2;top:0;left:-1px;right:-1px;height:40px;align-items:center;
      justify-content:center;background:#0b0b0d;border-bottom:1px solid var(--ui-border)}
    .carrow .ar{position:relative}
    .carrow .dn{color:var(--ui-accent)}
    .stage[data-hb-mode="compare"] .hname{display:block;position:absolute;top:0;left:8px}
    @container (max-width:200px){.stage[data-hb-mode="compare"] .card-meta{display:none}}
    @container (max-width:145px){.stage[data-hb-mode="compare"] .card>div:last-child{display:none}.stage[data-hb-mode="compare"] .card{justify-content:center;padding:10px 0}}
    @media(max-width:600px){.col-right{display:none}.zl br{display:inline}
      .stage[data-hb-mode="inside"] .col-mid{margin:0 80px 0 100px}}
    @media(prefers-reduced-motion:reduce){.col-inner{will-change:auto}
      /* the windows stand on their starts, so no start outline is drawn */
      .stage[data-hb-mode="inside"] .zs{display:none}}
  </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 data-hb-motion-note="The columns stay still while the box scrolls">
<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="../parallax-scrolling/" rel="prev" aria-label="Previous: Parallax 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>Parallax Scrolling</span></a><a href="../cover-card-to-fixed-header/" rel="next" aria-label="Next: Cover Card to Fixed Header"><span><span class="hb-dir">Next: </span>Cover Card to Fixed Header</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.03 · Scroll-Based</p>
    <h1>Reverse-Scrolling Columns</h1>
    <p class="hb-lede">Side columns run the opposite way to the middle one. Best for portfolios.</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="Three columns of project cards">
      <div class="scene">
        <div class="columns">
          <div class="half">
            <div class="col" aria-hidden="true">
              <div class="col-inner" id="inner-left"></div>
            </div>
            <div class="col col-mid">
              <div class="col-inner" id="inner-center"></div>
            </div>
            <div class="col col-right" aria-hidden="true">
              <div class="col-inner" id="inner-right"></div>
            </div>
          </div>
        </div>
      </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>Each column holds its cards twice, and the box is a window sliding down the middle and up the sides, then jumping a copy. Scroll the box or press Play to watch.</p>
      <p data-hb-for="compare" hidden>All one way moves every column up with the scroll, like a plain page; Sides reversed sends the side columns down against it. Scroll the box or press Play to watch.</p>
    </div>
  </section>

  <section class="hb-step hb-try" aria-labelledby="hb-try-title">
    <div class="hb-step-head">
      <span class="hb-num" aria-hidden="true">2</span>
      <div><h2 id="hb-try-title">Try it</h2><p>Change a setting, then scroll again or press Play.</p></div>
    </div>
    <div class="hb-settings">
      <div class="hb-setting">
        <p class="hb-setting-name" id="dir-lbl">Side columns go</p>
        <div class="seg" id="dir-seg" role="group" aria-labelledby="dir-lbl">
          <button type="button" data-dir="-1" class="on" aria-pressed="true">Opposite way</button>
          <button type="button" data-dir="1" aria-pressed="false">Same way</button>
        </div>
        <p class="hb-hint">Same way shows the plain scroll, for comparison.</p>
      </div>
      <div class="hb-setting">
        <p class="hb-setting-name" id="speed-lbl">Side column speed</p>
        <div class="seg" id="speed-seg" role="group" aria-labelledby="speed-lbl">
          <button type="button" data-speed="0.5" aria-pressed="false">Slower</button>
          <button type="button" data-speed="1" class="on" aria-pressed="true">Same</button>
          <button type="button" data-speed="1.5" aria-pressed="false">Faster</button>
        </div>
        <p class="hb-hint">Their speed compared with the middle column.</p>
      </div>
    </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">3</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 reverse-scrolling columns to [your grid of project cards or images]. Lay the cards out in three columns and hold them in view while the visitor scrolls through a taller section. Move the middle column up with the scroll, and move the two side columns in the direction and at the speed given in the settings; running them the opposite way makes them seem to flow against the scroll. Put two copies of the cards in each column and wrap the movement by exactly one copy's height, so the columns never run out. If the visitor has reduced motion turned on, keep the columns still. Match the settings listed below.</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 element. Your settings are added at the end.</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>Portfolios</li><li>Agency sites</li><li>Image galleries</li></ul>
      <h3>Avoid on</h3>
      <ul class="hb-tags hb-avoid"><li>Reading pages</li><li>Forms</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>
  const CARDS = [
    {s:'grid',    l:'Project Alpha',  m:'Design · 2024',   a:'#58a6ff'},
    {s:'circle',  l:'Studio Work',    m:'Motion · 2023',   a:'#bf91f3'},
    {s:'tri',     l:'Concept 03',     m:'Dev · 2024',      a:'#3fb950'},
    {s:'diamond', l:'Brand Atlas',    m:'Identity · 2023', a:'#f78166'},
    {s:'cross',   l:'Interface V2',   m:'UX · 2024',       a:'#ffa657'},
    {s:'arc',     l:'Motion Reel',    m:'Video · 2022',    a:'#79c0ff'},
    {s:'zz',      l:'Type Study',     m:'Type · 2023',     a:'#d2a8ff'},
    {s:'dot',     l:'Data Viz',       m:'Research · 2024', a:'#56d364'},
    {s:'lines',   l:'Scroll Kit',     m:'Library · 2023',  a:'#58a6ff'},
    {s:'hex',     l:'Space UI',       m:'Design · 2024',   a:'#bf91f3'},
    {s:'wave',    l:'Sound Tools',    m:'Audio · 2023',    a:'#3fb950'},
    {s:'ring',    l:'Orbit System',   m:'Motion · 2024',   a:'#f78166'},
  ];
  const SVG = {
    grid:   '<rect x="2" y="2" width="4" height="4" rx="1" fill="currentColor"/><rect x="8" y="2" width="4" height="4" rx="1" fill="currentColor" opacity=".45"/><rect x="2" y="8" width="4" height="4" rx="1" fill="currentColor" opacity=".45"/><rect x="8" y="8" width="4" height="4" rx="1" fill="currentColor"/>',
    circle: '<circle cx="7" cy="7" r="4" stroke="currentColor" stroke-width="1.5" fill="none"/><circle cx="7" cy="7" r="1.5" fill="currentColor"/>',
    tri:    '<polygon points="7,2 12,11 2,11" stroke="currentColor" stroke-width="1.5" fill="none"/>',
    diamond:'<polygon points="7,1 13,7 7,13 1,7" stroke="currentColor" stroke-width="1.5" fill="none"/>',
    cross:  '<line x1="7" y1="2" x2="7" y2="12" stroke="currentColor" stroke-width="1.5"/><line x1="2" y1="7" x2="12" y2="7" stroke="currentColor" stroke-width="1.5"/>',
    arc:    '<path d="M3 10 Q7 2 11 10" stroke="currentColor" stroke-width="1.5" fill="none"/>',
    zz:     '<polyline points="2,10 5,4 8,10 11,4" stroke="currentColor" stroke-width="1.5" fill="none"/>',
    dot:    '<circle cx="7" cy="7" r="2.5" fill="currentColor"/><circle cx="3" cy="11" r="1.5" fill="currentColor" opacity=".5"/><circle cx="11" cy="11" r="1.5" fill="currentColor" opacity=".5"/>',
    lines:  '<line x1="2" y1="5" x2="12" y2="5" stroke="currentColor" stroke-width="1.5"/><line x1="2" y1="9" x2="9" y2="9" stroke="currentColor" stroke-width="1.5"/>',
    hex:    '<polygon points="7,1 12,4 12,10 7,13 2,10 2,4" stroke="currentColor" stroke-width="1.5" fill="none"/>',
    wave:   '<path d="M2 7 C4 4 6 10 8 7 S12 4 12 7" stroke="currentColor" stroke-width="1.5" fill="none"/>',
    ring:   '<circle cx="7" cy="7" r="5" stroke="currentColor" stroke-width="2" fill="none"/><circle cx="7" cy="7" r="2" fill="currentColor" opacity=".35"/>',
  };

  function makeSet() {
    const div = document.createElement('div');
    div.className = 'card-set';
    div.innerHTML = CARDS.map(c =>
      `<div class="card">` +
      `<div class="icon-box" style="background:${c.a}22;color:${c.a}">` +
      `<svg width="14" height="14" viewBox="0 0 14 14" aria-hidden="true">${SVG[c.s]}</svg></div>` +
      `<div><div class="card-label">${c.l}</div>` +
      `<div class="card-meta">${c.m}</div></div></div>`
    ).join('');
    return div;
  }

  const stage       = document.getElementById('stage');
  const leftInner   = document.getElementById('inner-left');
  const centerInner = document.getElementById('inner-center');
  const rightInner  = document.getElementById('inner-right');
  const motionMQ    = window.matchMedia('(prefers-reduced-motion: reduce)');
  const phoneMQ     = window.matchMedia('(max-width: 600px)');
  const inners      = [leftInner, centerInner, rightInner];

  inners.forEach(el => {
    el.appendChild(makeSet());
    // The second set only repeats the first so the column can loop, and the side columns repeat the middle one: a screen reader hears the twelve cards once
    el.appendChild(makeSet()).setAttribute('aria-hidden', 'true');
  });

  // Side columns: dir -1 runs them the opposite way, +1 the same way; speed is their share of the middle column's.
  let dir = -1, speed = 1, mult = -1;
  let ticking = false, mode = 'normal';

  // Loop period = one card-set plus the 8px col-inner gap. Re-measured after the webfont lands, on resize and on every mode
  // change, because card height changes with the font and with the column's width (Compare's halves are narrower).
  let singleSetH = 1, twinSetH = 1, colW = 0;

  const el = (tag, cls, html) => { const e = document.createElement(tag); e.className = cls; if (html) e.innerHTML = html; return e; };
  const hide = e => { e.setAttribute('aria-hidden', 'true'); e.inert = true; return e; };
  const ty = v => `translate3d(0,${v}px,0)`;

  // Compare's left half: a copy of the columns, built once and without ids, in which every column moves up
  const mainHalf = stage.querySelector('.half'), twinHalf = mainHalf.cloneNode(true);
  twinHalf.querySelectorAll('[id]').forEach(e => e.removeAttribute('id'));
  twinHalf.classList.add('twin');
  mainHalf.before(hide(twinHalf));
  const mainCols = [...mainHalf.querySelectorAll('.col')], twinCols = [...twinHalf.querySelectorAll('.col')];
  const twinInners = twinCols.map(c => c.firstElementChild);

  // See inside, over each column: its two copies, the window, the window's start, the line it jumps from and an arrow; the
  // middle column carries the four labels
  const zooms = mainCols.map((col, i) => {
    const z = hide(el('div', 'zoom', '<i class="zc"></i><i class="zc"></i><i class="zs"></i><i class="zw"></i><i class="zj"></i><i class="ar"></i>'));
    if (i === 1) {
      z.insertAdjacentHTML('beforeend', '<p class="zl zl1" data-hb-caption>Copy 1</p><p class="zl zl2" data-hb-caption>Copy 2, <br>the same</p><p class="zl zlj" data-hb-caption>Jumps back <br>here</p>');
      z.querySelector('.zw').innerHTML = '<p class="zl zlb" data-hb-caption>In the box</p>';
    }
    col.append(z);
    return z;
  });
  const wins = zooms.map(z => z.querySelector('.zw'));

  // Compare: an arrow on every column and a name on each half
  [[twinCols, 'All one way'], [mainCols, 'Sides reversed']].forEach(([cols, name]) => {
    cols.forEach(c => c.append(hide(el('i', 'carrow', '<b class="ar"></b>'))));
    const n = hide(el('p', 'hname', name));
    n.setAttribute('data-hb-caption', '');
    cols[0].parentElement.append(n);
  });

  const PAD = 14;      // room above and below a zoomed column
  const POSE = 0.22;   // Compare under reduced motion: how far into a loop both halves stand
  const ARROW = 22;    // Compare's arrow on the middle column; a side column's is this times its speed
  let zk = 1;          // how much See inside shrinks a column

  function measure() {
    inners.concat(twinInners).forEach(e => { e.style.transform = 'none'; });
    colW = (stage.clientWidth - 16 - 8 * (phoneMQ.matches ? 1 : 2)) / (phoneMQ.matches ? 2 : 3);
    stage.style.setProperty('--zcw', colW + 'px');
    singleSetH = leftInner.children[0].getBoundingClientRect().height + 8;
    if (mode === 'compare') twinSetH = twinInners[0].children[0].getBoundingClientRect().height + 8;
    // Moving down, the side columns are pre-offset so set A sits above the box
    // and set B starts visible. Moving up they start at their own top: the
    // pre-offset would leave up to a box of empty column below the last card.
    // See inside holds every column still, so no pre-offset there.
    leftInner.style.marginTop = rightInner.style.marginTop = mode !== 'inside' && mult < 0 ? `-${singleSetH}px` : '0px';
  }

  // Where a set's columns stand for a scroll of st: the middle one scrolls with the input, content travelling up; the sides:
  // magnitude = speed, sign = direction (negative = down = reverse); all wrapped at one set's height P
  function slide(cols, st, P, m) {
    cols[1].style.transform = ty(-(st % P));
    const off = (st * Math.abs(m)) % P;
    cols[0].style.transform = cols[2].style.transform = ty(m >= 0 ? -off : off);
  }

  function update() {
    ticking = false;
    const still = motionMQ.matches;
    if (mode === 'normal') {
      // Reduced motion: columns stay put, and switching it on mid-visit sets them back at rest
      if (still) inners.forEach(e => { e.style.transform = ''; });
      else slide(inners, stage.scrollTop, singleSetH, mult);
    } else if (mode === 'inside') {
      // Each window is where the box is on its column: the middle one slides down; a side one slides up for Opposite way
      // (from the top of copy 2 to the top of copy 1) and down for Same way. Reduced motion leaves them on their starts.
      wins.forEach((w, i) => {
        const m = i === 1 ? 1 : mult, off = ((still ? 0 : stage.scrollTop) * Math.abs(m)) % singleSetH;
        w.style.transform = ty(PAD + (m >= 0 ? off : singleSetH - off) * zk);
      });
    } else {
      slide(twinInners, still ? POSE * twinSetH : stage.scrollTop, twinSetH, speed);
      slide(inners, still ? POSE * singleSetH : stage.scrollTop, singleSetH, mult);
    }
  }

  // See inside's picture: the real columns at one scale, set so both copies fit the box's height
  function layoutInside() {
    const hc = stage.clientHeight - 16, P = singleSetH;
    zk = (hc - 2 * PAD) / (2 * P - 8);
    const zs = P * zk, copy = (P - 8) * zk, win = hc * zk;
    stage.style.setProperty('--zsw', colW * zk + 'px');
    inners.forEach(e => { e.style.transform = `translate3d(0,${PAD}px,0) scale(${zk})`; });
    zooms.forEach((z, i) => {
      const side = i !== 1, up = side && dir < 0, len = (side ? speed : 1) * zs;   // up: the window travels up
      const q = s => z.querySelector(s), [c1, c2] = z.querySelectorAll('.zc'), a = q('.ar');
      c1.style.cssText = `top:${PAD}px;height:${copy}px`;
      c2.style.cssText = `top:${PAD + zs}px;height:${copy}px`;
      q('.zs').style.cssText = `height:${win}px;transform:${ty(PAD + (up ? P : 0) * zk)}`;
      q('.zw').style.height = win + 'px';
      // the line the window's top edge jumps from: the top of copy 2 going down, the top of copy 1 going up
      q('.zj').style.top = (PAD + (up ? 0 : zs) - 1) + 'px';
      // the arrow is as long as the window goes while the middle one goes one copy, from the strip's end the window leaves
      a.className = 'ar ' + (up ? 'up' : 'dn');
      a.style.cssText = `height:${len}px;top:${up ? PAD + 2 * zs - 8 * zk - len : PAD}px`;
      if (!side) {
        q('.zl1').style.top = (PAD + copy / 2) + 'px';
        q('.zl2').style.top = (PAD + zs + copy / 2) + 'px';
        q('.zlj').style.top = (PAD + zs) + 'px';
      }
    });
  }

  // Compare's arrows: the middle column goes up; a side column goes up on the left, and on the right down for Opposite way
  function layoutCompare() {
    [[twinCols, () => false], [mainCols, i => i !== 1 && dir < 0]].forEach(([cols, down]) => cols.forEach((c, i) => {
      const b = c.querySelector('.carrow b');
      b.style.height = Math.round(ARROW * (i === 1 ? 1 : speed)) + 'px';
      b.className = 'ar ' + (down(i) ? 'dn' : 'up');
    }));
  }

  const LABELS = {
    normal:  'Three columns of project cards',
    inside:  'Each column zoomed out to its two copies of the cards, with a window for what the box shows',
    compare: 'The columns twice: all moving one way, and with the side columns reversed',
  };
  const insideNote  = document.querySelector('[data-hb-for="inside"]');
  const compareNote = document.querySelector('[data-hb-for="compare"]');
  // The notes follow Side columns go and reduced motion, so they stay true to the picture
  const NOTES = {
    inside:           ['Each column holds its cards twice, and the box is a window sliding down the middle and up the sides, then jumping a copy. Scroll the box or press Play to watch.',
                       'Each column holds its cards twice, and with Same way picked the box is a window sliding down every column, then jumping a copy. Scroll the box or press Play to watch.',
                       'Shown still: each column holds its cards twice, and the box is a window on it; the arrows show which way each window would slide before jumping a copy.'],
    compare:          ['All one way moves every column up with the scroll, like a plain page; Sides reversed sends the side columns down against it. Scroll the box or press Play to watch.',
                       'Side columns go is set to Same way, so All one way and Sides reversed match. Pick Opposite way in Try it, then scroll the box or press Play to watch.',
                       'Shown still, part of the way down the box: in All one way every column has moved up, in Sides reversed the side columns have moved down.',
                       'Shown still, part of the way down the box; Side columns go is set to Same way, so All one way and Sides reversed match. Pick Opposite way in Try it to see the difference.'],
  };
  function notes() {
    const same = dir > 0, still = motionMQ.matches;
    insideNote.textContent  = NOTES.inside[still ? 2 : same ? 1 : 0];
    compareNote.textContent = NOTES.compare[still ? (same ? 3 : 2) : same ? 1 : 0];
  }

  function refresh() {
    measure();
    if (mode === 'inside') layoutInside(); else if (mode === 'compare') layoutCompare();
    notes();
    update();
  }

  document.addEventListener('hb:mode', e => {
    mode = e.detail.mode;
    stage.setAttribute('aria-label', LABELS[mode]);
    refresh();
  });
  stage.addEventListener('scroll', () => {
    if (!ticking) { requestAnimationFrame(update); ticking = true; }
  }, { passive: true });

  function choices(id, pick) {
    const buttons = document.querySelectorAll('#' + id + ' button');
    buttons.forEach(b => b.addEventListener('click', () => {
      buttons.forEach(x => { x.classList.toggle('on', x === b); x.setAttribute('aria-pressed', String(x === b)); });
      pick(b.dataset);
    }));
  }
  choices('dir-seg', d => { dir = +d.dir; mult = dir * speed; refresh(); });
  choices('speed-seg', d => { speed = +d.speed; mult = dir * speed; refresh(); });

  window.addEventListener('resize', refresh);
  // Reduced motion switched on or off mid-visit: clear the movement, or take it up again from where the box is
  motionMQ.addEventListener('change', () => { notes(); update(); });

  refresh();
  // The site font can land after the first measure and change the card height, so measure again once the fonts are ready
  if (document.fonts && document.fonts.ready) {
    document.fonts.ready.then(refresh);
  }
</script>
</body>
</html>