YL

SCROLL EFFECT

视差滚动Parallax Scrolling

背景、中景、前景以不同速度滚动,制造肉眼可感的纵深。

中英对照

中文
视差滚动
英文
Parallax Scrolling
别名
多层视差、Depth Scrolling
一句话
背景、中景、前景以不同速度滚动,制造肉眼可感的纵深。

效果预览

背景 0.3× / 中景 0.6× / 前景 1×
不同层以不同速度滚动

常见变体

鼠标视差Pointer Parallax

不跟滚动,而是各层跟随指针小幅反向平移,适合桌面端首屏插画;触屏没有悬停,直接关闭。

适合用在哪

Hero 区、品牌故事页、游戏与创意官网。

不适合:以阅读和操作为主的页面(文章正文、表单、后台、商品列表):内容和背景错位移动会干扰阅读,也会让一部分用户头晕;只有一张平面图、没有前后层次的画面也不必做,没有「近大远小」,位移只会被看成画面在晃。

和相近模式的区别

滚动驱动动画Scroll-linked Animation

视差是滚动驱动动画里最特殊的一种:映射的属性固定是 translateY,系数在整段滚动中不变,没有明确的起点和终点,目的只是拉出纵深;滚动驱动动画是在一段指定区间里把进度映射到缩放、透明度、宽度等任意属性,到区间终点停在终态。要让进度条填满、柱状图长高,用滚动驱动动画;只想让几层画面分出远近,用视差。

容易踩的坑

  • 在 scroll 事件里直接改 top 或 background-position,每帧重排导致严重卡顿。
  • 背景层速度系数过大且移动幅度超过 100px,容易引发晕动症,必须尊重减弱动效偏好。
  • 图层位移后露出容器边缘,背景图没有预留足够的超出尺寸。

设计提示词 · Prompt

视差滚动(Parallax Scrolling):至少三层——背景层以 0.2-0.4 倍速、中景 0.6 倍速、前景 1 倍速随滚动移动。实现:监听 scroll,对每层设 transform:translate3d(0, 区块已滚动距离 ×(1 − 速度), 0),前景 1 倍速即不位移,背景 0.3 倍速即位移 0.7 × 滚动距离(务必用 transform 而非 top,且加 will-change:transform)。更现代的方案是 CSS 滚动驱动动画:区块声明 view-timeline,各层用 animation-timeline 引用它,并配 animation-range: exit 0% exit 100%。悬停视差(鼠标视差)用 mousemove 让各层按指针偏移 2-6% 平移。层次元素大小要悬殊(近大远小)+ 前景层加投影,深度感才明显。移动端降级为静态或仅保留两层。
Parallax Scrolling: at least three layers — the background layer moves at 0.2-0.4x speed, the midground at 0.6x, and the foreground at 1x with scrolling. Implementation: listen to scroll and set transform:translate3d(0, sectionScrolled × (1 − speed), 0) on each layer, so the 1x foreground gets no offset and a 0.3x background moves 0.7 × the scrolled distance (always use transform rather than top, and add will-change:transform). A more modern approach is CSS scroll-driven animation: declare view-timeline on the section, reference it from each layer with animation-timeline, and set animation-range: exit 0% exit 100%. Hover parallax (mouse parallax) uses mousemove to shift each layer by 2-6% according to the pointer offset. Layer elements should differ greatly in size (near large, far small) + the foreground layer gets a drop shadow for the depth to be noticeable. On mobile, degrade to static or keep only two layers.

可复制 CSS

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

/* <section class="parallax">
     <div class="parallax__layer parallax__layer--far"><img src="sky.jpg" alt=""></div>
     <div class="parallax__layer parallax__layer--mid"><img src="hills.png" alt=""></div>
     <div class="parallax__content">前景文案:1 倍速,正常滚动</div>
   </section>
   按页面顶部的 Hero 计算。不支持滚动驱动动画时,JS 把区块滚出视口顶部的距离(0 到区块高度,单位 px)写进 --parallax-y */
.parallax {
  position: relative;
  height: 80vh;
  overflow: hidden;
  view-timeline: --parallax block;
}
.parallax__layer {
  position: absolute;
  inset: 0;
  will-change: transform;
  transform: translate3d(0, calc(var(--parallax-y, 0px) * (1 - var(--speed, 1))), 0);
}
.parallax__layer > img { display: block; width: 100%; height: 100%; object-fit: cover; }
.parallax__layer--far { --speed: .3; }
.parallax__layer--mid { --speed: .6; }
.parallax__content {
  position: relative;
  z-index: 1;
  display: grid;
  place-content: center;
  height: 100%;
  color: #fff;
  font: 700 clamp(32px, 5vw, 64px)/1.1 system-ui, sans-serif;
  text-align: center;
  text-shadow: 0 2px 16px rgba(0, 0, 0, .35);
}
@supports (animation-timeline: view()) {
  .parallax__layer {
    animation: parallax-shift linear both;
    animation-timeline: --parallax;
    animation-range: exit 0% exit 100%;
  }
}
@keyframes parallax-shift {
  from { transform: translate3d(0, 0, 0); }
  to { transform: translate3d(0, calc((1 - var(--speed, 1)) * 100%), 0); }
}
@media (max-width: 640px) {
  .parallax__layer--mid { --speed: 1; }
}
@media (prefers-reduced-motion: reduce) {
  .parallax__layer {
    animation: none;
    transform: none;
    will-change: auto;
  }
}

演示 HTML 结构

<div class="s s--abs d-parallax">
  <span class="d-parallax__tag">背景 0.3× / 中景 0.6× / 前景 1×</span>
  <div class="d-parallax__mid"></div>
  <div class="d-parallax__front"></div>
  <div class="d-parallax__card">不同层以不同速度滚动</div>
</div>

无障碍与性能

无障碍

  • 视差是前庭障碍用户最容易不适的动效之一。它由滚动触发,不属于 SC 2.2.2 的「自动开始」,但 SC 2.3.3(AAA 级)要求交互触发的动效可以关闭:prefers-reduced-motion: reduce 时各层回到静止,JS 兼容方案也要在同一条件下不注册滚动监听。
  • 文字放在不位移的前景层,随页面正常滚动;背景和中景只放装饰图,用空 alt=""。文字压在会移动的图上,要按图层滑到最亮的位置检查对比度。
  • 层的位移不改变 DOM 顺序,读屏顺序和 Tab 顺序仍按 HTML 顺序。

性能

  • 只改 transform。支持滚动驱动动画的浏览器里,transform 动画可以在合成线程跟随滚动,不占主线程;JS 方案在主线程繁忙时会比滚动慢一拍,看起来像在抖。
  • JS 方案的 scroll 监听加 { passive: true },回调里只调度一次 requestAnimationFrame,每帧最多写一次 --parallax-y;读取的是区块本身的位置,区块不做 transform,不会读到上一帧的位移。
  • will-change: transform 只加在两三个视差层上,不要扩散到层里的每个子元素;移动端把中景的 --speed 设为 1,只保留背景一层位移。

常见问题

视差滚动的英文是什么?

视差滚动的英文是 Parallax Scrolling。常见英文别名还包括 Depth Scrolling。

Parallax Scrolling 的中文是什么?

Parallax Scrolling 的中文是「视差滚动」。常见中文别名还包括 多层视差。背景、中景、前景以不同速度滚动,制造肉眼可感的纵深。

视差滚动的速度系数和位移怎么换算?

前景层跟着页面正常滚动,就是 1 倍速,不需要位移。要让背景看起来以 0.3 倍速移动,它要反方向「追」回 70%:位移 = 已滚动距离 ×(1 − 0.3)。本条写成 calc(var(--parallax-y) * (1 - var(--speed))),--speed 填的就是看起来的倍速。如果直接写 scrollY × 0.3,背景实际是以 0.7 倍速在动;前景按 scrollY × 1 写,反而会钉在原地不动。

视差滚动最少需要哪些 JS?

先用 CSS.supports('animation-timeline: view()') 判断,支持就不用写。不支持时:window.addEventListener('scroll', onScroll, { passive: true }),onScroll 里只调度一次 requestAnimationFrame;在 rAF 里读区块的 getBoundingClientRect().top,把 Math.min(Math.max(-top, 0), 区块高度) 写进 --parallax-y。用 IntersectionObserver 在区块离开视口时移除滚动监听、回来再加上;命中 prefers-reduced-motion: reduce 时整段不执行。

background-attachment: fixed 能代替视差吗?

它只能做出最简单的两层:背景图相对视口固定(相当于 0 倍速),内容从图上滑过,系数不能调,也没有中景。iOS Safari 至今只部分支持 fixed 背景,通常按普通滚动处理;在部分浏览器里,fixed 背景还可能让滚动时整块区域重绘、拖慢滚动。要可调的多层效果,还是用 transform。

常见搭配

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

标签