{T}

滚动捕捉与平滑滚动

概述

本文档系统讲解 滚动捕捉与平滑滚动 中的各个属性。每个属性都配有来自 MDN 的可交互演示,可以直接点击运行查看效果。

滚动捕捉

scroll-snap-type

在滚动容器上启用滚动捕捉并指定捕捉方向。

scroll-snap-type 交互演示(MDN)

在滚动容器上启用滚动捕捉并指定捕捉方向。

html
<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>scroll-snap-type 属性演示 - MDN 示例</title>
    <meta name="description" content="演示scroll(snap-type 属性演示)效果。" />
    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }
      body {
        font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
        min-height: 100vh;
        padding: 0;
      }
      .demo-layout {
        display: flex;
        height: 100vh;
        gap: 0;
      }
      .snippet-panel {
        width: 320px;
        flex-shrink: 0;
        padding: 16px;
        display: flex;
        flex-direction: column;
        gap: 8px;
        overflow-y: auto;
        border-right: 1px solid #ddd;
        background: #fafafa;
      }
      .snippet-btn {
        padding: 10px 14px;
        border: 1px solid #ccc;
        border-radius: 6px;
        background: #fff;
        font-family: "JetBrains Mono", "Fira Code", monospace;
        font-size: 13px;
        color: #333;
        cursor: pointer;
        text-align: left;
        transition: all 0.2s;
        line-height: 1.4;
      }
      .snippet-btn:hover {
        border-color: #8083ff;
        background: #f0f0ff;
      }
      .snippet-btn.active {
        border-color: #8083ff;
        background: #e8e8ff;
        color: #571bc1;
        font-weight: 600;
      }
      .preview-panel {
        flex: 1;
        padding: 16px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: #fff;
        overflow: auto;
      }
      .preview-panel > section,
      .preview-panel > div:not(.snippet-panel):not(.demo-layout) {
        flex: 1;
        width: 100%;
        min-height: 0;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
      }
      .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: #fff;
        font-size: 30px;
        display: flex;
        align-items: center;
        justify-content: center;
        scroll-snap-align: start;
      }
 
      #example-element > div:nth-child(even) {
        background-color: #fff;
        color: rebeccapurple;
      }
    </style>
  </head>
  <body>
    <div class="demo-layout">
      <div class="snippet-panel">
        <button class="snippet-btn active" data-index="0">scroll-snap-type: none;</button>
        <button class="snippet-btn" data-index="1">scroll-snap-type: x mandatory;</button>
        <button class="snippet-btn" data-index="2">scroll-snap-type: x proximity;</button>
      </div>
      <div class="preview-panel">
        <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>
      </div>
    </div>
    <script>
      const snippets = [
        `#example-element {
  scroll-snap-type: none;
}`,
        `#example-element {
  scroll-snap-type: x mandatory;
}`,
        `#example-element {
  scroll-snap-type: x proximity;
}`,
      ];
 
      let styleEl = document.createElement("style");
      document.head.appendChild(styleEl);
 
      function applySnippet(index) {
        styleEl.textContent = snippets[index];
        document.querySelectorAll(".snippet-btn").forEach((btn, i) => {
          btn.classList.toggle("active", i === index);
        });
      }
 
      document.querySelectorAll(".snippet-btn").forEach((btn) => {
        btn.addEventListener("click", () => applySnippet(parseInt(btn.dataset.index)));
      });
 
      applySnippet(0);
    </script>
  </body>
</html>
 

scroll-snap-align

设置子元素在滚动捕捉容器中的对齐位置。

scroll-snap-align 交互演示(MDN)

设置子元素在滚动捕捉容器中的对齐位置。

html
<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>scroll-snap-align 属性演示 - MDN 示例</title>
    <meta name="description" content="演示scroll(snap-align 属性演示)效果。" />
    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }
      body {
        font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
        min-height: 100vh;
        padding: 0;
      }
      .demo-layout {
        display: flex;
        height: 100vh;
        gap: 0;
      }
      .snippet-panel {
        width: 320px;
        flex-shrink: 0;
        padding: 16px;
        display: flex;
        flex-direction: column;
        gap: 8px;
        overflow-y: auto;
        border-right: 1px solid #ddd;
        background: #fafafa;
      }
      .snippet-btn {
        padding: 10px 14px;
        border: 1px solid #ccc;
        border-radius: 6px;
        background: #fff;
        font-family: "JetBrains Mono", "Fira Code", monospace;
        font-size: 13px;
        color: #333;
        cursor: pointer;
        text-align: left;
        transition: all 0.2s;
        line-height: 1.4;
      }
      .snippet-btn:hover {
        border-color: #8083ff;
        background: #f0f0ff;
      }
      .snippet-btn.active {
        border-color: #8083ff;
        background: #e8e8ff;
        color: #571bc1;
        font-weight: 600;
      }
      .preview-panel {
        flex: 1;
        padding: 16px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: #fff;
        overflow: auto;
      }
      .preview-panel > section,
      .preview-panel > div:not(.snippet-panel):not(.demo-layout) {
        flex: 1;
        width: 100%;
        min-height: 0;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
      }
      .default-example {
        flex-wrap: wrap;
      }
 
      .default-example .info {
        width: 100%;
        padding: 0.5em 0;
        font-size: 90%;
      }
 
      #example-parent {
        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;
      }
 
      #example-parent > div {
        flex: 0 0 66%;
        width: 250px;
        background-color: rebeccapurple;
        color: #fff;
        font-size: 30px;
        display: flex;
        align-items: center;
        justify-content: center;
      }
 
      #example-parent > div:nth-child(even) {
        background-color: #fff;
        color: rebeccapurple;
      }
    </style>
  </head>
  <body>
    <div class="demo-layout">
      <div class="snippet-panel">
        <button class="snippet-btn active" data-index="0">scroll-snap-align: start;</button>
        <button class="snippet-btn" data-index="1">scroll-snap-align: end;</button>
        <button class="snippet-btn" data-index="2">scroll-snap-align: center;</button>
      </div>
      <div class="preview-panel">
        <section class="default-example" id="default-example">
          <div id="example-parent">
            <div>1</div>
            <div id="example-element">2</div>
            <div>3</div>
          </div>
          <div class="info">Scroll »</div>
        </section>
      </div>
    </div>
    <script>
      const snippets = [
        `#example-element {
  scroll-snap-align: start;
}`,
        `#example-element {
  scroll-snap-align: end;
}`,
        `#example-element {
  scroll-snap-align: center;
}`,
      ];
 
      let styleEl = document.createElement("style");
      document.head.appendChild(styleEl);
 
      function applySnippet(index) {
        styleEl.textContent = snippets[index];
        document.querySelectorAll(".snippet-btn").forEach((btn, i) => {
          btn.classList.toggle("active", i === index);
        });
      }
 
      document.querySelectorAll(".snippet-btn").forEach((btn) => {
        btn.addEventListener("click", () => applySnippet(parseInt(btn.dataset.index)));
      });
 
      applySnippet(0);
    </script>
  </body>
</html>
 

scroll-snap-stop

设置滚动捕捉是否允许跳过中间捕捉位置(normal/always)。

scroll-snap-stop 交互演示(MDN)

设置滚动捕捉是否允许跳过中间捕捉位置(normal/always)。

html
<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>scroll-snap-stop 属性演示 - MDN 示例</title>
    <meta name="description" content="演示scroll(snap-stop 属性演示)效果。" />
    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }
      body {
        font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
        min-height: 100vh;
        padding: 0;
      }
      .demo-layout {
        display: flex;
        height: 100vh;
        gap: 0;
      }
      .snippet-panel {
        width: 320px;
        flex-shrink: 0;
        padding: 16px;
        display: flex;
        flex-direction: column;
        gap: 8px;
        overflow-y: auto;
        border-right: 1px solid #ddd;
        background: #fafafa;
      }
      .snippet-btn {
        padding: 10px 14px;
        border: 1px solid #ccc;
        border-radius: 6px;
        background: #fff;
        font-family: "JetBrains Mono", "Fira Code", monospace;
        font-size: 13px;
        color: #333;
        cursor: pointer;
        text-align: left;
        transition: all 0.2s;
        line-height: 1.4;
      }
      .snippet-btn:hover {
        border-color: #8083ff;
        background: #f0f0ff;
      }
      .snippet-btn.active {
        border-color: #8083ff;
        background: #e8e8ff;
        color: #571bc1;
        font-weight: 600;
      }
      .preview-panel {
        flex: 1;
        padding: 16px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: #fff;
        overflow: auto;
      }
      .preview-panel > section,
      .preview-panel > div:not(.snippet-panel):not(.demo-layout) {
        flex: 1;
        width: 100%;
        min-height: 0;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
      }
      .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: #fff;
        font-size: 30px;
        display: flex;
        align-items: center;
        justify-content: center;
        scroll-snap-align: start;
      }
 
      .snap-container > div:nth-child(even) {
        background-color: #fff;
        color: rebeccapurple;
      }
    </style>
  </head>
  <body>
    <div class="demo-layout">
      <div class="snippet-panel">
        <button class="snippet-btn active" data-index="0">scroll-snap-stop: normal;</button>
        <button class="snippet-btn" data-index="1">scroll-snap-stop: always;</button>
      </div>
      <div class="preview-panel">
        <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>
      </div>
    </div>
    <script>
      const snippets = [
        `#example-element {
  scroll-snap-stop: normal;
}`,
        `#example-element {
  scroll-snap-stop: always;
}`,
      ];
 
      let styleEl = document.createElement("style");
      document.head.appendChild(styleEl);
 
      function applySnippet(index) {
        styleEl.textContent = snippets[index];
        document.querySelectorAll(".snippet-btn").forEach((btn, i) => {
          btn.classList.toggle("active", i === index);
        });
      }
 
      document.querySelectorAll(".snippet-btn").forEach((btn) => {
        btn.addEventListener("click", () => applySnippet(parseInt(btn.dataset.index)));
      });
 
      applySnippet(0);
    </script>
  </body>
</html>
 

平滑滚动

scroll-behavior

设置容器滚动行为:instant 直接跳转或 smooth 平滑滚动。

scroll-behavior 交互演示(MDN)

设置容器滚动行为:instant 直接跳转或 smooth 平滑滚动。

html
<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>scroll-behavior 属性演示 - MDN 示例</title>
    <meta name="description" content="演示scroll(behavior 属性演示)效果。" />
    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }
      body {
        font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
        min-height: 100vh;
        padding: 0;
      }
      .demo-layout {
        display: flex;
        height: 100vh;
        gap: 0;
      }
      .snippet-panel {
        width: 320px;
        flex-shrink: 0;
        padding: 16px;
        display: flex;
        flex-direction: column;
        gap: 8px;
        overflow-y: auto;
        border-right: 1px solid #ddd;
        background: #fafafa;
      }
      .snippet-btn {
        padding: 10px 14px;
        border: 1px solid #ccc;
        border-radius: 6px;
        background: #fff;
        font-family: "JetBrains Mono", "Fira Code", monospace;
        font-size: 13px;
        color: #333;
        cursor: pointer;
        text-align: left;
        transition: all 0.2s;
        line-height: 1.4;
      }
      .snippet-btn:hover {
        border-color: #8083ff;
        background: #f0f0ff;
      }
      .snippet-btn.active {
        border-color: #8083ff;
        background: #e8e8ff;
        color: #571bc1;
        font-weight: 600;
      }
      .preview-panel {
        flex: 1;
        padding: 16px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: #fff;
        overflow: auto;
      }
      .preview-panel > section,
      .preview-panel > div:not(.snippet-panel):not(.demo-layout) {
        flex: 1;
        width: 100%;
        min-height: 0;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
      }
      .container {
        flex-direction: column;
      }
 
      .nav a {
        color: #009e5f;
      }
 
      scroll-container {
        border: 1px solid black;
        display: block;
        height: 200px;
        overflow-y: scroll;
        width: 200px;
      }
 
      scroll-page {
        align-items: center;
        display: flex;
        font-size: 5em;
        height: 100%;
        justify-content: center;
      }
    </style>
  </head>
  <body>
    <div class="demo-layout">
      <div class="snippet-panel">
        <button class="snippet-btn active" data-index="0">scroll-behavior: auto;</button>
        <button class="snippet-btn" data-index="1">scroll-behavior: smooth;</button>
      </div>
      <div class="preview-panel">
        <section id="default-example">
          <div class="container">
            <p class="nav">
              Scroll to:
              <a href="#pageA">A</a>
              <a href="#pageB">B</a>
              <a href="#pageC">C</a>
            </p>
            <scroll-container id="example-element">
              <scroll-page id="pageA">A</scroll-page>
              <scroll-page id="pageB">B</scroll-page>
              <scroll-page id="pageC">C</scroll-page>
            </scroll-container>
          </div>
        </section>
      </div>
    </div>
    <script>
      const snippets = [
        `#example-element {
  scroll-behavior: auto;
}`,
        `#example-element {
  scroll-behavior: smooth;
}`,
      ];
 
      let styleEl = document.createElement("style");
      document.head.appendChild(styleEl);
 
      function applySnippet(index) {
        styleEl.textContent = snippets[index];
        document.querySelectorAll(".snippet-btn").forEach((btn, i) => {
          btn.classList.toggle("active", i === index);
        });
      }
 
      document.querySelectorAll(".snippet-btn").forEach((btn) => {
        btn.addEventListener("click", () => applySnippet(parseInt(btn.dataset.index)));
      });
 
      applySnippet(0);
    </script>
  </body>
</html>