scroll-snap-type

Baseline 已广泛支持

此功能已经非常成熟,可在多种设备和浏览器版本上使用。自 2022 年 4 月以来,它已在各大浏览器中得到支持。

scroll-snap-type CSS 属性设置在滚动容器上,通过设置吸附端口内吸附点强制执行的方向和严格程度,使其选择滚动吸附。

试一试

scroll-snap-type: none;
scroll-snap-type: x mandatory;
scroll-snap-type: x proximity;
<section class="default-example" id="default-example">
  <div id="example-element">
    <div>1</div>
    <div>2</div>
    <div>3</div>
  </div>
  <div class="info">Scroll »</div>
</section>
.default-example {
  flex-wrap: wrap;
}

.default-example .info {
  width: 100%;
  padding: 0.5em 0;
  font-size: 90%;
}

#example-element {
  text-align: left;
  width: 250px;
  height: 250px;
  overflow-x: scroll;
  display: flex;
  box-sizing: border-box;
  border: 1px solid black;
}

#example-element > div {
  flex: 0 0 250px;
  width: 250px;
  background-color: rebeccapurple;
  color: white;
  font-size: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  scroll-snap-align: start;
}

#example-element > div:nth-child(even) {
  background-color: white;
  color: rebeccapurple;
}

如果滚动视口中的内容发生变化(例如,如果内容被添加、移动、删除或调整大小),则如果该内容仍然存在,滚动容器将重新吸附到先前吸附的内容。

如果与滚动吸附相关的属性(例如 scroll-snap-typescroll-margin)的值发生更改,滚动容器将根据 scroll-snap-type 的当前值重新吸附。

精确动画或用于强制执行这些吸附点的物理原理的指定不属于此属性的范围,而是由用户代理决定。

语法

css
/* No snapping */
scroll-snap-type: none;

/* Keyword values for snap axes */
scroll-snap-type: x;
scroll-snap-type: y;
scroll-snap-type: block;
scroll-snap-type: inline;
scroll-snap-type: both;

/* Optional keyword values for snap strictness */
/* mandatory | proximity */
scroll-snap-type: x mandatory;
scroll-snap-type: y proximity;
scroll-snap-type: both mandatory;

/* Global values */
scroll-snap-type: inherit;
scroll-snap-type: initial;
scroll-snap-type: revert;
scroll-snap-type: revert-layer;
scroll-snap-type: unset;

none

当此滚动容器的视觉视口滚动时,它必须忽略吸附点。

x

滚动容器仅在其水平轴上吸附到吸附位置。

y

滚动容器仅在其垂直轴上吸附到吸附位置。

block

滚动容器仅在其块轴上吸附到吸附位置。

inline

滚动容器仅在其行内轴上吸附到吸附位置。

both

滚动容器在其两个轴上独立吸附到吸附位置(可能在每个轴上吸附到不同的元素)。

强制

如果此滚动容器的视觉视口当前未滚动,则它必须吸附到吸附位置。

临近

如果此滚动容器的视觉视口当前未滚动,则它可能会吸附到吸附位置。用户代理根据滚动参数决定是否吸附。如果指定了任何吸附轴,这是默认的吸附严格程度。

正式定义

初始值none
应用于所有元素
继承性
计算值同指定值
动画类型离散

正式语法

scroll-snap-type = 
none |
[ x | y | block | inline | both ] [ mandatory | proximity ]?

示例

在不同轴上吸附

HTML

html
<main>
  <section class="x mandatory-scroll-snapping" dir="ltr">
    <div>X Mand. LTR</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
  </section>
  <section class="x proximity-scroll-snapping" dir="ltr">
    <div>X Prox. LTR</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
  </section>
  <section class="y mandatory-scroll-snapping" dir="ltr">
    <div>Y Mand. LTR</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
  </section>
  <section class="y proximity-scroll-snapping" dir="ltr">
    <div>Y Prox. LTR</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
  </section>
  <section class="x mandatory-scroll-snapping" dir="rtl">
    <div>X Mand. RTL</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
  </section>
  <section class="x proximity-scroll-snapping" dir="rtl">
    <div>X Prox. RTL</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
  </section>
  <section class="y mandatory-scroll-snapping" dir="rtl">
    <div>Y Mand. RTL</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
  </section>
  <section class="y proximity-scroll-snapping" dir="rtl">
    <div>Y Prox. RTL</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
  </section>
</main>

CSS

css
/* scroll-snap */
.x.mandatory-scroll-snapping {
  scroll-snap-type: x mandatory;
}
.x.proximity-scroll-snapping {
  scroll-snap-type: x proximity;
}
.y.mandatory-scroll-snapping {
  scroll-snap-type: y mandatory;
}
.y.proximity-scroll-snapping {
  scroll-snap-type: y proximity;
}

div {
  text-align: center;
  scroll-snap-align: center;
  flex: none;
}

结果

规范

规范
CSS 滚动捕捉模块级别 1
# scroll-snap-type

浏览器兼容性

另见