EN
把灵感留住
滚动 · 原生 JS + CSS

粘性分屏叙事

Sticky scrollytelling

左边的图留在原处,右边说明滚过一段,配图就换一张

常见大白话搜法:左边图片固定右边说明一段段滚过、滚动产品介绍时左侧配图跟着换、一边固定图片一边滚动文字、粘性分屏叙事产品介绍

AI 专用 Markdown (.md)
适用位置
图片、正文
气质感觉
高级、柔和
触发方式
滚动
适合网站
品牌官网、工具和应用

实现原理与无障碍底线

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

适用场景:产品功能介绍、使用步骤和案例讲解:读到哪一段,就看到那一段的配图

减少动态(prefers-reduced-motion):不固定配图、不淡入淡出,图和说明全部静态排列

移动端与触屏:两列保留,文字不要太长;手指原生上下滚动,减少动态时直接阅读全部图文

关键可调参数

参数键名默认值建议范围说明
配图换过去的时长duration400ms——
缓动easingcubic-bezier(0.4, 0, 0.2, 1)——

可直接复制给 AI(Cursor / Claude / v0)的提示词

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

原生无依赖参考代码

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; }
}
JavaScript
// 粘性分屏叙事 · 原生 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;
}
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>

同属「滚动」的其他动效

查看全部 86 个动效 →

本次更新仅弹窗提醒一次 · 可随时在历史公告回看