YL

SCROLL EFFECT

滚动驱动动画Scroll-linked Animation

动画进度直接绑定滚动位置:滚动条就是时间轴,倒着滚动画也倒着放。

中英对照

中文
滚动驱动动画
英文
Scroll-linked Animation
别名
滚动进度动画、Scroll Timeline、滚动绑定
一句话
动画进度直接绑定滚动位置:滚动条就是时间轴,倒着滚动画也倒着放。

效果预览

动画进度 ↔ 滚动位置双向绑定

适合用在哪

数据展示区、产品特性长页、时间线叙事。

不适合:只想让元素出现一次的场景:用滚动入场揭示更简单,滚动驱动动画往回滚会倒放,用户回看时元素又消失一遍;价格、条款这类必须被读完的关键信息也不要绑在滚动上,用户停在半路时,内容就停在半成品状态(文字半透明、柱子只长一半)。

和相近模式的区别

视差滚动Parallax Scrolling

视差只是滚动驱动的一种用法:几层画面按固定倍速平移,没有终点,只为拉出纵深;滚动驱动动画讲的是「这段滚动距离对应一段完整变化」,有明确的起止区间(animation-range),可以映射到缩放、透明度、进度等任意属性,变化本身在传达信息,比如读到了哪里、数据长到多少。

滚动驱动动画Scroll-Driven Animations

mc-scroll-driven 条目讲的是 animation-timeline、scroll()、view() 这组 CSS 能力本身的语法和兼容性;本条是设计模式,讲什么内容适合绑在滚动上、区间怎么取,以及不支持时用 JS 怎么兜底。

容易踩的坑

  • 只写 animation-timeline 不做 @supports 回退,Firefox 等不支持的浏览器元素停在初始态。
  • JS 兼容方案在 scroll 回调里读写布局交替,触发强制同步布局造成掉帧。
  • 动画绑定区间过短,轻轻一滚就播完,用户来不及看清变化过程。

设计提示词 · Prompt

滚动驱动动画(Scroll-linked Animation):CSS 原生方案——@keyframes 配合 animation-timeline:scroll()(相对整页滚动)或 view()(相对元素进入视口),animation-range 控制起止区间,无需 JS。兼容方案用 JS:监听 scroll 计算元素在视口中的进度 p∈[0,1],把 p 映射到 transform/scale/宽度等属性。典型应用:进度条随滚动填充、柱状图随滚动长高(scaleY 0→1)、图片随滚动从 0.6 放大到 1。倒滚时动画自然反向——这是它与一次性入场动画的本质区别。
Scroll-linked Animation: native CSS approach — @keyframes together with animation-timeline:scroll() (relative to whole-page scrolling) or view() (relative to the element entering the viewport), with animation-range controlling the start and end range, no JS required. Fallback with JS: listen to scroll, compute the element's progress p∈[0,1] in the viewport, and map p to transform/scale/width and other properties. Typical uses: a progress bar filling with scroll, bar charts growing with scroll (scaleY 0→1), images scaling from 0.6 to 1 with scroll. Scrolling backward naturally reverses the animation — this is the essential difference from a one-time entrance animation.

可复制 CSS

自包含的最小实现,粘贴到空白页面即可生效;类名可以按项目规范改名。

/* 阅读进度条:<div class="read-progress" aria-hidden="true"></div>(放在 <body> 末尾)
   柱状图:<div class="grow-bars"><span style="--h:40%"></span><span style="--h:72%"></span>…</div>
   不支持滚动驱动动画时,JS 把整页滚动进度(0 到 1)写进 <html> 的 --scroll-progress;柱状图不支持时直接显示最终高度 */
.read-progress {
  position: fixed;
  inset: 0 0 auto;
  z-index: 50;
  height: 3px;
  background: #4f46e5;
  transform-origin: 0 50%;
  transform: scaleX(var(--scroll-progress, 0));
}
.grow-bars {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  height: 200px;
  view-timeline: --bars block;
}
.grow-bars > span {
  flex: 1;
  height: var(--h, 50%);
  border-radius: 6px 6px 0 0;
  background: #4f46e5;
  transform-origin: 50% 100%;
}
@supports (animation-timeline: view()) {
  .read-progress {
    animation: progress-grow linear both;
    animation-timeline: scroll(root block);
  }
  .grow-bars > span {
    animation: bar-grow linear both;
    animation-timeline: --bars;
    animation-range: entry 20% cover 50%;
  }
}
@keyframes progress-grow {
  from { transform: scaleX(0); }
  to { transform: scaleX(1); }
}
@keyframes bar-grow {
  from { transform: scaleY(0); }
  to { transform: scaleY(1); }
}
@media (prefers-reduced-motion: reduce) {
  .grow-bars > span {
    animation: none;
    transform: none;
  }
}

演示 HTML 结构

<div class="s d-scrolldrive">
  <div class="d-scrolldrive__rail"><div class="d-scrolldrive__fill"></div><div class="d-scrolldrive__knob"></div></div>
  <div class="d-scrolldrive__bar"><i style="animation-delay:0s"></i><i style="animation-delay:.12s"></i><i style="animation-delay:.24s"></i><i style="animation-delay:.36s"></i><i style="animation-delay:.48s"></i></div>
  <span class="d-scrolldrive__tag">动画进度 ↔ 滚动位置双向绑定</span>
</div>

无障碍与性能

无障碍

  • 阅读进度条只是视觉提示,加 aria-hidden="true",不要让读屏把它当成进度控件播报。
  • 柱状图的数值要以文字形式存在(柱子上的标签,或旁边的数据表),动画只是呈现方式;不支持滚动驱动动画或减弱动效时,柱子直接是最终高度。
  • prefers-reduced-motion: reduce 时本条保留阅读进度条:它完全跟随用户自己的滚动,只是一条 3px 的细线,不是大面积运动;柱状图长高则关闭。如果产品要求减弱动效时一律静止,也可以把进度条一起隐藏。

性能

  • 原生滚动驱动动画只动画 transform 时,可以在合成线程跟随滚动,不需要监听 scroll,也不占主线程。
  • JS 兼容方案:scroll 监听加 { passive: true },在 requestAnimationFrame 里先统一读(scrollY、scrollHeight、innerHeight),再统一写 --scroll-progress。
  • 进度条用 scaleX 而不是改 width:改 width 每帧都会触发布局。

常见问题

滚动驱动动画的英文是什么?

滚动驱动动画的英文是 Scroll-linked Animation。常见英文别名还包括 Scroll Timeline。

Scroll-linked Animation 的中文是什么?

Scroll-linked Animation 的中文是「滚动驱动动画」。常见中文别名还包括 滚动进度动画、滚动绑定。动画进度直接绑定滚动位置:滚动条就是时间轴,倒着滚动画也倒着放。

animation-timeline 写了却不生效是什么原因?

最常见的是写在了 animation 简写前面:animation 简写会把 animation-timeline 重置为 auto,所以 animation-timeline 必须写在 animation 之后,本条就是这个顺序。第二个常见原因是祖先元素设置了 overflow: hidden:它会成为最近的滚动容器,view() 和 scroll(nearest) 绑到这个滚不动的容器上,进度就卡在一个固定值;只是想裁切内容的话,改用 overflow: clip,它不会创建滚动容器。

滚动驱动动画的 JS 兼容方案怎么写?

先用 CSS.supports('animation-timeline: scroll()') 判断,支持就不加载脚本。不支持时:scroll 监听加 { passive: true },回调里只调度一次 requestAnimationFrame;在 rAF 里计算 p = scrollY ÷(document.documentElement.scrollHeight − innerHeight),限制在 0 到 1,用 document.documentElement.style.setProperty('--scroll-progress', p) 写入。元素级的进度用 IntersectionObserver 只在元素可见期间计算,离开视口后移除监听。

animation-range 里的 entry、cover、contain、exit 有什么区别?

都按元素和视口的相对位置划分:cover 是从元素顶边碰到视口底部,到元素底边离开视口顶部的全过程;entry 是从底部进入的那一段;exit 是从顶部离开的那一段;contain 是元素完整待在视口里的那一段(元素比视口高时,是它铺满视口的那一段)。本条柱状图写 entry 20% cover 50%:容器露出 20% 时开始长,滚到视口正中时长满。

常见搭配

同一页面里经常和「滚动驱动动画」一起出现的模式,可以收藏后用「组合收藏为整页提示词」一起交给 AI。

标签