scroll-snap-stop

Baseline 已广泛支持

此功能已成熟,并可在许多设备和浏览器版本上运行。自 2022 年 7 月以来,所有浏览器都已支持此功能。

scroll-snap-stop CSS 属性定义滚动容器是否允许“跳过”可能的吸附位置。

试一试

scroll-snap-stop: normal;
scroll-snap-stop: always;
<section class="default-example" id="default-example">
  <p class="explanation">
    The effect of this property can be noticed on devices with a touchpad. Try
    to scroll through all items with a single swing. Value
    <b class="keyword">'normal'</b> should pass through all pages, while
    <b class="keyword">'always'</b> will stop at the second page.
  </p>
  <div class="snap-container">
    <div>1</div>
    <div id="example-element">2</div>
    <div>3</div>
  </div>
  <div class="info">Scroll »</div>
</section>
.default-example {
  flex-direction: column;
}

.explanation {
  margin-top: 0;
}

.keyword {
  color: darkorange;
}

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

.snap-container {
  text-align: left;
  width: 250px;
  height: 250px;
  overflow-x: scroll;
  display: flex;
  box-sizing: border-box;
  border: 1px solid black;
  scroll-snap-type: x mandatory;
}

.snap-container > 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;
}

.snap-container > div:nth-child(even) {
  background-color: white;
  color: rebeccapurple;
}

语法

css
/* Keyword values */
scroll-snap-stop: normal;
scroll-snap-stop: always;

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

normal

当此元素的滚动容器的视觉视口滚动时,它可能会“跳过”可能的吸附位置。

always

滚动容器不得“跳过”可能的吸附位置;并且必须吸附到此元素吸附位置的第一个。

正式定义

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

正式语法

scroll-snap-stop = 
normal |
always

示例

设置不同的吸附停止点

下面的示例演示了 scroll-snap-stopalwaysnormal 值之间的对比。当 scroll-snap-type 属性设置为 mandatory 时(此示例中就是如此),两个 scroll-snap-stop 值之间的差异会更明显。

HTML

html
<p>scroll-snap-stop: always (X Mandatory)</p>
<div class="x mandatory-scroll-snapping always-stop">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</div>

<p>scroll-snap-stop: always (X Mandatory) on odd child elements</p>
<div class="x mandatory-scroll-snapping always-stop-odd">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</div>

<p>scroll-snap-stop: always (X Mandatory) on even child elements</p>
<div class="x mandatory-scroll-snapping always-stop-even">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</div>

<p>scroll-snap-stop: normal (X Mandatory)</p>
<div class="x mandatory-scroll-snapping normal-stop">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</div>

<p>scroll-snap-stop: always (Y Mandatory)</p>
<div class="y mandatory-scroll-snapping always-stop">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</div>

<p>scroll-snap-stop: normal (Y Mandatory)</p>
<div class="y mandatory-scroll-snapping normal-stop">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</div>

CSS

css
/* setting up mandatory scroll-snap on parent */
.x.mandatory-scroll-snapping {
  scroll-snap-type: x mandatory;
}

.y.mandatory-scroll-snapping {
  scroll-snap-type: y mandatory;
}

/* defining scroll-snap alignment on children */
div > div {
  scroll-snap-align: center;
}

/* defining scroll-snap stop on children */
.always-stop > div {
  scroll-snap-stop: always;
}

.always-stop-odd > div:nth-of-type(odd) {
  scroll-snap-stop: always;
}

.always-stop-even > div:nth-of-type(even) {
  scroll-snap-stop: always;
}

.normal-stop > div {
  scroll-snap-stop: normal;
}

结果

分别在下面的 X 和 Y 框中从左到右和从上到下滚动。在 scroll-snap-stop 属性设置为 always 的 X 和 Y 框中,即使快速滚动,也会强制滚动在吸附点停止。但是,在 scroll-snap-stop 属性设置为 normal 的框中,快速滚动时会跳过吸附点。

如果需要,您可以有选择性地确定滚动容器内始终停止的项。下面的示例通过定位奇数和偶数项来演示这一点;您可以根据您的要求选择不同的策略。在下面的示例中,滚动不会分别跳过第二个和第三个框中的奇数和偶数项。

规范

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

浏览器兼容性

另见