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

瀏覽器相容性

另見