视差滚动
Parallax
滚动时前景和后景走不同速度,画面因此有了纵深。
- 又叫:
- 视差
- 层叠滚动
- 分层滚动
- 视差效果
- parallax scrolling
- multi-layer scroll
- scroll depth
- 展厅
- 动效与节奏
- 形态
- 自带滚动
- 播放
- 手动触发
- 关键词
- 需要 JS / 新 CSS 特性 / 随滚动变化 / 重绘开销 / 动效较多 / 可以直接用
参数工作台
复制给 AI
随参数实时更新给这个滚动容器做视差滚动(parallax):背景层按滚动距离乘 0.45 偏移,中景和前景各按不同系数偏移,基准位移 60px。用 transform: translate3d 实现,scroll 里用 requestAnimationFrame 节流,只在滚动期间加 will-change: transform。prefers-reduced-motion 下把系数归零。
parallax scrolling, multi-layer scroll depth, each layer translate3d(0, scrollY * 0.45 + 60px, 0), background slower than foreground, driven by requestAnimationFrame inside a passive scroll listener, will-change: transform only while scrolling, respect prefers-reduced-motion
https://publieople.github.io/design-museum/entry/parallax/?stage=light
链接带着你调好的参数
什么时候用
- 首屏头图想比正文走得慢一点,制造「往里走」的感觉
- 多图层插画、品牌页、活动页,想用层次代替真 3D
- 卡片列表滚过时让背景或图片轻微错位,增加质感
适用场景:滚动 / 页面 / 卡片
推荐取值
- 前后景速度比
- 0.3–0.6(背景慢、前景快)
- 单层最大位移
- 不超过容器高度的 50%
- 层级数量
- 2–4 层,再多看不出区别
- 驱动方式
- requestAnimationFrame 或 animation-timeline: scroll()
别搞混
和「fixed 背景」的区别
background-attachment: fixed 是背景相对视口钉住不动,只有一层;视差是每一层用各自的 transform 速度移动,可以有三四层,而且能被 JS 控制。
和「滚动驱动动画 scroll-driven」的区别
视差只管「位移随滚动线性变化」;滚动驱动动画可以把滚动进度映射到任意属性(旋转、缩放、颜色),还能用 animation-range 控制起止区间。
容易踩的坑
- 位移距离给太大会让人晕,前后景速度差控制在 20%–50% 就够,超过就没法读内容
- scroll 监听里直接读 scrollY 会抖动,用 requestAnimationFrame 节流或 CSS 的 animation-timeline: scroll()
- 移动端和 prefers-reduced-motion 下最好直接关掉,iOS 的滚动回弹会让视差明显跳一下
- 给它加 will-change: transform 只在动画期间加,常驻会一直占着合成层显存
无障碍降级
prefers-reduced-motion: reduce 时把位移速度全部置 0,让所有图层停在原位,页面照常可读。
关键词
- parallax scrolling
- transform: translate3d
- will-change: transform
- animation-timeline: scroll()
- background-attachment: fixed
- scroll-linked