1. 交互
  2. 滚动捕捉对齐

交互

scroll-snap-align

用于控制元素滚动捕捉对齐的工具。

ClassStyles
snap-start
scroll-snap-align: start;
snap-end
scroll-snap-align: end;
snap-center
scroll-snap-align: center;
snap-align-none
scroll-snap-align: none;

示例

中间捕捉

使用 snap-center 工具在滚动到捕捉容器内部时将元素捕捉到其中心:

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

捕捉点
<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-start 工具在滚动到捕捉容器内部时将元素捕捉到其开始位置:

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

捕捉点
<div class="snap-x ...">
<div class="snap-start ...">
<img src="/img/vacation-01.jpg" />
</div>
<div class="snap-start ...">
<img src="/img/vacation-02.jpg" />
</div>
<div class="snap-start ...">
<img src="/img/vacation-03.jpg" />
</div>
<div class="snap-start ...">
<img src="/img/vacation-04.jpg" />
</div>
<div class="snap-start ...">
<img src="/img/vacation-05.jpg" />
</div>
<div class="snap-start ...">
<img src="/img/vacation-06.jpg" />
</div>
</div>

结束捕捉

使用 snap-end 工具在滚动到捕捉容器内部时将元素捕捉到其结束位置:

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

捕捉点
<div class="snap-x ...">
<div class="snap-end ...">
<img src="/img/vacation-01.jpg" />
</div>
<div class="snap-end ...">
<img src="/img/vacation-02.jpg" />
</div>
<div class="snap-end ...">
<img src="/img/vacation-03.jpg" />
</div>
<div class="snap-end ...">
<img src="/img/vacation-04.jpg" />
</div>
<div class="snap-end ...">
<img src="/img/vacation-05.jpg" />
</div>
<div class="snap-end ...">
<img src="/img/vacation-06.jpg" />
</div>
</div>

响应式设计

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

<div class="snap-center md:snap-start ...">
<!-- ... -->
</div>

Learn more about using variants in the variants documentation.