# 粘性分屏叙事（Sticky scrollytelling）

- 俗称：左边图片固定右边说明一段段滚过；滚动产品介绍时左侧配图跟着换；一边固定图片一边滚动文字；粘性分屏叙事产品介绍
- 用在：图片、正文（品牌官网、工具和应用）
- 触发：滚动
- 感觉：高级、柔和
- 版本：1，更新于 2026-11-13

## 原理

图文分成两列，左侧用 sticky 保持在滚动框顶部，右侧每段占一屏；按滚动距离除以窗口高选中最近一段，对应配图按时长和缓动淡入，其余淡出并退出读屏；滚轮、触摸、方向键都能读完，减少动态时全部图文静态排列。

适合：产品功能介绍、使用步骤和案例讲解：读到哪一段，就看到那一段的配图。

## 参数

| 参数名 | 含义 | 默认值 | 建议范围 |
|---|---|---|---|
| `duration` | 配图换过去的时长 | 400ms | 150–1000ms |
| `easing` | 缓动 | cubic-bezier(0.4, 0, 0.2, 1) | — |

## 质量要求

- 开启"减少动态"时：不固定配图、不淡入淡出，图和说明全部静态排列。
- 手机上：两列保留，文字不要太长；手指原生上下滚动，减少动态时直接阅读全部图文。
- 动画只在播放时改变元素；脚本出错或没有执行时，内容照样可见（不要先用 CSS 把内容藏起来再等脚本显示）。

## 提示词（可以直接交给 AI）

```text
请在我的项目里给图片加上「粘性分屏叙事」（Sticky scrollytelling）效果：图文分成两列，左侧用 sticky 保持在滚动框顶部，右侧每段占一屏；按滚动距离除以窗口高选中最近一段，对应配图按时长和缓动淡入，其余淡出并退出读屏；滚轮、触摸、方向键都能读完，减少动态时全部图文静态排列。
参数：配图换过去的时长 400ms，缓动 cubic-bezier(0.4, 0, 0.2, 1)。
要求：保留原有的内容和布局；系统开启"减少动态"时直接显示最终状态；脚本出错或没有执行时内容仍然可见。
参考：https://donglang.linter.top/effects/scrollytelling.md（原理、参数和参考代码）
```

## 参考代码

HTML 结构：

```html
<section class="st" tabindex="0" aria-label="产品故事：滚动或按方向键浏览">
  <div class="st-layout">
    <div class="st-visual">
      <figure class="st-picture"><img src="capture.jpg" alt="把想法收进收件箱"></figure>
      <figure class="st-picture"><img src="organize.jpg" alt="整理成有主题的笔记"></figure>
      <figure class="st-picture"><img src="publish.jpg" alt="从笔记发布文章"></figure>
    </div>
    <div class="st-steps">
      <section class="st-step"><h2>随手记</h2><p>捕捉今天的灵感。</p></section>
      <section class="st-step"><h2>慢慢理</h2><p>把相关的笔记放在一起。</p></section>
      <section class="st-step"><h2>写出来</h2><p>让想法成为可以分享的作品。</p></section>
    </div>
  </div>
</section>
<script type="module">
  import { scrollytelling } from './scrollytelling.js';
  const story = scrollytelling(document.querySelector('.st'), { duration: 400 });
  // 路由卸载时 story.destroy()。
</script>
```

CSS：

```css
/* 先静态展示全部图文；脚本增强后左图粘住、右文滚动。 */
.st { --st-height: 70vh; --st-duration: 400ms; --st-easing: cubic-bezier(0.4, 0, 0.2, 1); }
.st-layout { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 24px; }
.st-picture { margin: 0; }
.st-picture img { display: block; width: 100%; height: 100%; object-fit: cover; }
.st-step { box-sizing: border-box; padding: 24px 8px; }
.st.st-js { height: var(--st-height); overflow: auto; position: relative; }
.st-js .st-visual { position: sticky; top: 0; align-self: start; height: var(--st-vh); }
.st-js .st-picture { position: absolute; inset: 0; opacity: 0; transition: opacity var(--st-duration) var(--st-easing); }
.st-js .st-step { min-height: var(--st-vh); display: flex; flex-direction: column; justify-content: center; }
.st:focus-visible { outline: 2px solid currentColor; outline-offset: -3px; }
@media (prefers-reduced-motion: reduce) {
  .st.st-js:not(.st-force) { height: auto; overflow: visible; }
  .st-js:not(.st-force) .st-visual { position: static; height: auto; }
  .st-js:not(.st-force) .st-picture { position: static; opacity: 1 !important; transition: none; }
  .st-js:not(.st-force) .st-step { min-height: 0; }
}
```

JS（原生，不依赖任何库）：

```js
// 粘性分屏叙事 · 原生 JS，本站独立实现，没有复制第三方代码。
// .st > .st-layout 内是 .st-visual（若干 .st-picture）和 .st-steps（同数量 .st-step）。
// root 是有高度的纵向滚动框；左边 sticky，右边每段高一屏；跨过半屏就选中下一张，按 duration/easing 淡入淡出。
// 高度由 --st-height 控制；root 上的 tabindex/aria-label 让滚轮、触摸和键盘都能操作。
// 没有 JS、减少动态时图文全部静态排列；ResizeObserver 和媒体查询变化会重排。
// 必须图文数量相等且非空；不满足时不改页面，返回 null。返回 update(), destroy()；destroy 后不再更新。
const instances = new WeakMap();
export function scrollytelling(root, {
  duration = 400,
  easing = 'cubic-bezier(0.4, 0, 0.2, 1)',
  ignoreReducedMotion = false,
} = {}) {
  instances.get(root)?.destroy();
  const pictures = [...root.querySelectorAll('.st-picture')];
  const steps = [...root.querySelectorAll('.st-step')];
  if (!pictures.length || pictures.length !== steps.length) return null;
  const saved = [root, ...pictures].map(el => [el, el.getAttribute('style'), el.getAttribute('aria-hidden'), el.hasAttribute('inert')]);
  const restore = () => saved.forEach(([el, style, aria, inert]) => {
    if (style === null) { void el.getAttribute('style'); el.removeAttribute('style'); } else el.setAttribute('style', style);
    if (aria === null) el.removeAttribute('aria-hidden'); else el.setAttribute('aria-hidden', aria);
    el.toggleAttribute('inert', inert);
  });
  const media = matchMedia('(prefers-reduced-motion: reduce)');
  let dead = false, frame = 0, current = -1;
  const update = () => {
    if (dead) return;
    const reduce = media.matches && !ignoreReducedMotion;
    root.classList.toggle('st-js', !reduce); root.classList.toggle('st-force', ignoreReducedMotion);
    if (reduce) { restore(); current = -1; return; }
    if (!(root.clientWidth > 0 && root.clientHeight > 0)) return;
    const h = `${root.clientHeight}px`;
    if (root.style.getPropertyValue('--st-vh') !== h) root.style.setProperty('--st-vh', h);
    root.style.setProperty('--st-duration', `${duration}ms`); root.style.setProperty('--st-easing', easing);
    const next = Math.max(0, Math.min(steps.length - 1, Math.floor(root.scrollTop / root.clientHeight + 0.5)));
    if (next === current) return;
    current = next;
    pictures.forEach((el, i) => { el.style.opacity = i === next ? '1' : '0'; el.setAttribute('aria-hidden', String(i !== next)); el.toggleAttribute('inert', i !== next); });
    root.dispatchEvent(new CustomEvent('storychange', { detail: { index: next } }));
  };
  const schedule = () => { if (!dead && !frame) frame = requestAnimationFrame(() => { frame = 0; update(); }); };
  const key = (e) => {
    if (e.target !== root || e.altKey || e.metaKey || e.ctrlKey) return;
    const step = root.clientHeight;
    const next = { ArrowDown: root.scrollTop + step, ArrowRight: root.scrollTop + step, PageDown: root.scrollTop + step,
      ArrowUp: root.scrollTop - step, ArrowLeft: root.scrollTop - step, PageUp: root.scrollTop - step,
      Home: 0, End: root.scrollHeight - root.clientHeight }[e.key];
    if (next === undefined) return;
    e.preventDefault(); root.scrollTop = next; update();
  };
  root.addEventListener('scroll', schedule, { passive: true }); root.addEventListener('keydown', key); media.addEventListener('change', schedule);
  const observer = new ResizeObserver(schedule); observer.observe(root);
  const ctl = { update, destroy() {
    if (dead) return; dead = true; cancelAnimationFrame(frame); observer.disconnect();
    root.removeEventListener('scroll', schedule); root.removeEventListener('keydown', key); media.removeEventListener('change', schedule);
    // 先停过渡再还原，避免拆掉时又播放一段。
    pictures.forEach(el => el.style.transition = 'none'); void root.offsetHeight;
    root.classList.remove('st-js', 'st-force'); restore();
    if (instances.get(root) === ctl) instances.delete(root);
  } };
  instances.set(root, ctl); update(); return ctl;
}
```

## 出处

- 同类效果见第四批清单中的 Animation Handbook scrollytelling 收录名；本站独立实现，没有复制代码、文字或图片；sticky、按半屏切换和键盘处理均为本站编写
