YL

SCROLL EFFECT

滚动吸附分页Scroll Snap

滑动松手后自动吸附到最近一页,像原生 App 的翻页,纯 CSS 零 JS。

中英对照

中文
滚动吸附分页
英文
Scroll Snap
别名
滚动快照、CSS 滚动捕捉、翻页滚动
一句话
滑动松手后自动吸附到最近一页,像原生 App 的翻页,纯 CSS 零 JS。

效果预览

第一屏松手自动吸附
第二屏scroll-snap-align
第三屏零 JS 实现

常见变体

竖向全屏翻页Full-screen Vertical Snap

每屏一个章节,上下滑动整屏翻页,适合活动页和产品发布页;内容可能超过一屏的页面改用 proximity。

适合用在哪

移动端 Banner 轮播、全屏分段叙事、图片浏览器。

不适合:需要自由浏览、频繁回看的长内容(文章、商品列表、数据报表):竖向 mandatory 吸附会抢走滚动的控制权,用户想停在两屏之间看图表时会被弹走;桌面端整页翻屏式的官网也要谨慎,触控板惯性滚动下的吸附手感常常不稳定,一次滑动可能被弹回或卡顿。

和相近模式的区别

横向滚动区Horizontal Scroll Section

横向滚动区一屏露出 2-4 张固定宽度的卡片,吸附只让停下的位置对齐卡片边缘,重点是「浏览」;滚动吸附分页每次只显示一整页(子项 100% 宽),配 scroll-snap-stop: always 一次只翻一页,重点是「翻页」,通常要配页码圆点告诉用户在第几页、一共几页。

容易踩的坑

  • 竖向全屏吸附配 mandatory,单屏内容超过 100vh 时下半部分永远滚不到。
  • 指示圆点在 scroll 事件里不节流计算索引,快速滑动时频繁重绘且圆点闪烁。
  • 移动端用 100vh 做每屏高度,地址栏伸缩导致吸附位置错位,应改用 100dvh。

设计提示词 · Prompt

滚动吸附分页(Scroll Snap):容器 overflow-x:auto + scroll-snap-type:x mandatory(强制吸附)或 proximity(接近才吸);子项 scroll-snap-align:start/center,scroll-snap-stop:always 防止一次滑过两页。配 indicator 圆点用 scroll 事件计算当前页索引。竖直全屏翻页版:容器 height:100dvh(先写 height:100vh 兜底)+ overflow-y:auto + scroll-snap-type:y mandatory,每屏 min-height:100dvh;任何一屏内容可能超过视口时改用 proximity。这是最被低估的 CSS 能力——轮播的 90% 场景根本不需要 JS 库。
Scroll Snap: container overflow-x:auto + scroll-snap-type:x mandatory (forced snapping) or proximity (snaps only when close); children use scroll-snap-align:start/center, and scroll-snap-stop:always prevents skipping two pages in one swipe. Pair with indicator dots, computing the current page index from scroll events. Vertical full-screen paging version: container height:100dvh (with height:100vh first as a fallback) + overflow-y:auto + scroll-snap-type:y mandatory, each screen min-height:100dvh; switch to proximity if any screen may be taller than the viewport. This is the most underrated CSS capability — 90% of carousel scenarios don't need a JS library at all.

可复制 CSS

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

/* <div class="snap" role="region" aria-roledescription="轮播" aria-label="新品" tabindex="0">
     <div class="snap__page" role="group" aria-roledescription="幻灯片" aria-label="第 1 页,共 3 页">…</div>(3 页)
   </div>
   <div class="snap__dots"><button type="button" aria-label="第 1 页" aria-current="true"></button>…</div> */
.snap {
  display: flex;
  overflow-x: auto;
  border-radius: 16px;
  scroll-snap-type: x mandatory;
  overscroll-behavior-x: contain;
  scroll-behavior: smooth;
  scrollbar-width: none;
}
.snap::-webkit-scrollbar { display: none; }
.snap:focus-visible { outline: 2px solid #6366f1; outline-offset: 2px; }
.snap__page {
  flex: 0 0 100%;
  min-height: 240px;
  scroll-snap-align: start;
  scroll-snap-stop: always;
}
.snap__dots { display: flex; justify-content: center; gap: 4px; margin-top: 8px; }
/* 按钮 24×24 是点击热区,里面的圆点只有 8px */
.snap__dots > button {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
}
.snap__dots > button::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: #6b7280;
  transition: width .2s ease, background-color .2s ease;
}
.snap__dots > button[aria-current="true"]::before { width: 18px; background: #111827; }
.snap__dots > button:focus-visible { outline: 2px solid #6366f1; outline-offset: -2px; border-radius: 6px; }
@media (prefers-reduced-motion: reduce) {
  .snap { scroll-behavior: auto; }
  .snap__dots > button::before { transition: none; }
}

演示 HTML 结构

<div class="s">
  <div class="d-snap">
    <div class="d-snap__page" style="background:var(--i-gd)"><b>第一屏</b><span>松手自动吸附</span></div>
    <div class="d-snap__page" style="background:linear-gradient(135deg,#0891b2,#4f46e5)"><b>第二屏</b><span>scroll-snap-align</span></div>
    <div class="d-snap__page" style="background:#17171b"><b>第三屏</b><span>零 JS 实现</span></div>
  </div>
  <div class="d-snap__dots"><i></i><i></i><i></i></div>
</div>

无障碍与性能

无障碍

  • 翻页完全由用户操作,不自动播放,不涉及 SC 2.2.2;如果加自动轮播,就必须提供可见的暂停按钮,并在聚焦或悬停时停止。
  • 圆点的视觉只有 8px,按钮本身做成 24×24px,满足 SC 2.5.8 的最小目标尺寸;当前页用 aria-current="true",并且比其他圆点更宽,不只靠颜色区分。
  • 容器加 tabindex="0",键盘聚焦后可以用左右方向键翻页;每页用「第 1 页,共 3 页」这样的标签,读屏用户知道自己在哪。

性能

  • 吸附由浏览器原生完成,不需要 JS 计算目标位置,也不需要监听 touchmove。
  • 当前页用 IntersectionObserver 判断,比在每个 scroll 事件里计算 scrollLeft ÷ clientWidth 更省;圆点宽度过渡只影响 24px 按钮内部的伪元素,布局开销可以忽略。

常见问题

滚动吸附分页的英文是什么?

滚动吸附分页的英文是 Scroll Snap。

Scroll Snap 的中文是什么?

Scroll Snap 的中文是「滚动吸附分页」。常见中文别名还包括 滚动快照、CSS 滚动捕捉、翻页滚动。滑动松手后自动吸附到最近一页,像原生 App 的翻页,纯 CSS 零 JS。

滚动吸附的圆点指示器最少需要哪些 JS?

两件事。同步当前页:new IntersectionObserver(callback, { root: 轮播容器, threshold: 0.6 }) 观察每一页,isIntersecting 为 true 的那一页对应的圆点设 aria-current="true",其余移除。点击圆点:调用 container.scrollTo({ left: 序号 × container.clientWidth }),不传 behavior,交给 CSS 的 scroll-behavior 决定是否平滑。也可以监听 scrollend 事件,在滚动停下后再计算索引,但它的浏览器支持不如 IntersectionObserver 完整,需要备用方案。

scroll-snap-type 用 mandatory 还是 proximity?

mandatory 表示滚动停下时一定落在某个吸附点上,适合每页等宽的横向轮播;proximity 只在停得离吸附点足够近时才吸过去,用户可以停在两页之间,适合长短不一的内容和竖向页面。两者都只决定「停在哪」,滑动时的手感仍是原生滚动。

CSS 能直接生成轮播的圆点和箭头吗?

较新的 Chromium 已支持 ::scroll-marker 和 ::scroll-button() 伪元素,可以由 CSS 为每一页生成可点击的标记点和前后翻页按钮,并自动标记当前页;Firefox、Safari 的支持情况请查 caniuse。现阶段仍建议以本条的 JS 圆点为默认方案,再用 @supports 做渐进增强。

常见搭配

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

标签