1. 交互
  2. 滚动捕捉类型

交互

scroll-snap-type

控制快照容器中快照点严格程度的实用工具。

ClassStyles
snap-none
scroll-snap-type: none;
snap-x
scroll-snap-type: x var(--tw-scroll-snap-strictness);
snap-y
scroll-snap-type: y var(--tw-scroll-snap-strictness);
snap-both
scroll-snap-type: both var(--tw-scroll-snap-strictness);
snap-mandatory
--tw-scroll-snap-strictness: mandatory;
snap-proximity
--tw-scroll-snap-strictness: proximity;

示例

水平滚动快照

使用 snap-x 实用工具在元素内启用水平滚动快照:

在图片网格中滚动以查看预期行为

快照点
<div class="snap-x ...">
<div class="snap-center ...">
<img src="/img/vacation-01.jpg" />
</div>
<div class="snap-center ...">
<img src="/img/vacation-02.jpg" />
</div>
<div class="snap-center ...">
<img src="/img/vacation-03.jpg" />
</div>
<div class="snap-center ...">
<img src="/img/vacation-04.jpg" />
</div>
<div class="snap-center ...">
<img src="/img/vacation-05.jpg" />
</div>
<div class="snap-center ...">
<img src="/img/vacation-06.jpg" />
</div>
</div>

要使滚动快照工作,您还需要在子元素上设置 滚动快照对齐

强制滚动快照

使用 snap-mandatory 实用工具强制快照容器始终停靠在快照点上:

在图片网格中滚动以查看预期行为

快照点
<div class="snap-x snap-mandatory ...">
<div class="snap-center ...">
<img src="/img/vacation-01.jpg" />
</div>
<div class="snap-center ...">
<img src="/img/vacation-02.jpg" />
</div>
<div class="snap-center ...">
<img src="/img/vacation-03.jpg" />
</div>
<div class="snap-center ...">
<img src="/img/vacation-04.jpg" />
</div>
<div class="snap-center ...">
<img src="/img/vacation-05.jpg" />
</div>
<div class="snap-center ...">
<img src="/img/vacation-06.jpg" />
</div>
</div>

邻近滚动快照

使用 snap-proximity 实用工具使快照容器停驻在靠近的快照点上:

在图片网格中滚动以查看预期行为

快照点
<div class="snap-x snap-proximity ...">
<div class="snap-center ...">
<img src="/img/vacation-01.jpg" />
</div>
<div class="snap-center ...">
<img src="/img/vacation-02.jpg" />
</div>
<div class="snap-center ...">
<img src="/img/vacation-03.jpg" />
</div>
<div class="snap-center ...">
<img src="/img/vacation-04.jpg" />
</div>
<div class="snap-center ...">
<img src="/img/vacation-05.jpg" />
</div>
</div>

响应式设计

Prefix a scroll-snap-type utility with a breakpoint variant like md: to only apply the utility at medium screen sizes and above:

<div class="snap-none md:snap-x ...">
<!-- ... -->
</div>

Learn more about using variants in the variants documentation.