scroll-snap-stop
scroll-snap-stop
CSS 属性定义了是否允许滚动容器“越过”可能的捕捉位置。
试一试
语法
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;
值
正式定义
正式语法
scroll-snap-stop =
normal |
always
示例
设置不同的捕捉停止
下面的示例演示了scroll-snap-stop
的always
和normal
值的对比。当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
的框中,快速滚动时会跳过捕捉点。
如果需要,您可以选择性地控制哪些项目在滚动容器内始终被always
停止。下面的示例演示了如何通过定位奇数和偶数项目来实现这一点;您可以根据您的需求选择不同的策略。在下面的示例中,滚动分别不会“越过”第二个和第三个方框中的奇数和偶数项目。
规范
规范 |
---|
CSS Scroll Snap 模块级别 1 # scroll-snap-stop |
浏览器兼容性
BCD 表格仅在浏览器中加载