網站導覽

v2.1.0
版面配置

黏附堆疊

隨著頁面捲動,面板會逐一在前一張下方停住並疊起來。

本頁內容

黏附堆疊由一小組面板組成,面板會隨頁面捲動逐一疊起來。每張面板捲到頂端時,都會在比前一張略低的位置停住,下一張則會滑過來蓋住它。已讀面板的邊緣仍會露在目前閱讀的面板上方。捲到堆疊底部時,所有面板會一起離開檢視區,接著顯示後續的頁面內容。

這是一種版面配置,負責決定哪些項目要黏附,以及黏附的位置,本身不繪製任何內容。每張面板都是一張卡片或一塊表面,用來遮住底下的面板。整個效果無須腳本。每張面板都使用 position: sticky,面板之間的偏移量由樣式表計算。

黏附堆疊

<div class="bg-surface-base fg-text-body text-sm">  <a class="skip" href="#stack-main">Skip to content</a>  <header class="navbar" style="--navbar-padding-x: max(1.5rem, calc(50% - 21rem))">    <a class="navbar-brand" href="#top">Atlas</a>    <nav class="navbar-nav" aria-label="Main">      <a class="navbar-link" href="#product">Product</a>      <a class="navbar-link" href="#start" aria-current="page">Get started</a>      <a class="navbar-link" href="#pricing">Pricing</a>    </nav>    <div class="navbar-end">      <a class="btn btn-primary btn-sm" href="#sign-up">Sign up</a>      <button type="button" class="btn btn-quiet btn-square btn-sm navbar-toggle" popovertarget="stack-menu" aria-label="Open the menu">        <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">          <path d="M3 5.5L13 5.5" /><path d="M3 10.5L13 10.5" />        </svg>      </button>      <div class="menu menu-end" id="stack-menu" popover>        <a class="menu-item" href="#product">Product</a>        <a class="menu-item" href="#start" aria-current="page">Get started</a>        <a class="menu-item" href="#pricing">Pricing</a>      </div>    </div>  </header>  <main class="flex flex-col gap-2xl max-w(720) mx-auto px-lg py-2xl" id="stack-main" tabindex="-1">    <header class="flex flex-col gap-xs">      <p class="eyebrow">Get started</p>      <h1 class="heading heading-2xl">From a repository to a live site</h1>      <p class="lead">        Four steps, about five minutes. Each one is undone as easily as it is done.      </p>    </header>    <ol class="sticky-stack" style="--sticky-stack-top: 4.5rem">      <li class="sticky-stack-item">        <article class="card min-h(256)">          <p class="eyebrow">Step 1</p>          <header class="card-header">            <h2 class="card-title">Connect a repository</h2>            <p class="card-description">Choose one you have, or start from a template.</p>          </header>          <div class="card-body well flex items-center justify-between gap-sm p-md">            <code class="code">atlas/site</code>            <span class="badge">main</span>          </div>          <footer class="card-footer">            <a class="btn btn-sm" href="#repositories">Choose another</a>          </footer>        </article>      </li>      <li class="sticky-stack-item">        <article class="card min-h(256)">          <p class="eyebrow">Step 2</p>          <header class="card-header">            <h2 class="card-title">Set the build</h2>            <p class="card-description">              The command that builds the site, and the folder it writes.            </p>          </header>          <div class="card-body well flex flex-col gap-3xs p-md">            <code class="code">bun run build</code>            <code class="code">dist/</code>          </div>          <footer class="card-footer">            <a class="btn btn-sm" href="#build">Edit the build</a>          </footer>        </article>      </li>      <li class="sticky-stack-item">        <article class="card min-h(256)">          <p class="eyebrow">Step 3</p>          <header class="card-header">            <h2 class="card-title">Preview every branch</h2>            <p class="card-description">              A pull request gets a link of its own, rebuilt on every push.            </p>          </header>          <div class="card-body well flex items-center justify-between gap-sm p-md">            <code class="code">pr-142.atlas.dev</code>            <span class="badge badge-success badge-dot">Ready</span>          </div>          <footer class="card-footer">            <a class="btn btn-sm" href="#preview">Open the preview</a>          </footer>        </article>      </li>      <li class="sticky-stack-item">        <article class="card min-h(256)">          <p class="eyebrow">Step 4</p>          <header class="card-header">            <h2 class="card-title">Ship</h2>            <p class="card-description">              Merge to main, and the preview you approved is what goes live.            </p>          </header>          <div class="card-body well flex items-center justify-between gap-sm p-md">            <code class="code">atlas.dev</code>            <span class="badge badge-success badge-live">Live</span>          </div>          <footer class="card-footer">            <a class="btn btn-sm btn-primary" href="#ship">Merge and ship</a>          </footer>        </article>      </li>    </ol>    <section class="flex flex-col gap-xs" aria-labelledby="stack-next">      <h2 class="heading heading-lg" id="stack-next">Then it is yours</h2>      <p class="m(0) leading-md">        Add a domain, invite the team, or leave it as it is. The stack has ended, so the steps scrolled away together, and the page goes on as a page.      </p>    </section>  </main></div>
import MenuIcon from '@master/icons-react/native/16/soft/menu'<div className="bg-surface-base fg-text-body text-sm">  <a className="skip" href="#stack-main">Skip to content</a>  <header className="navbar" style={{ '--navbar-padding-x': 'max(1.5rem, calc(50% - 21rem))' } as React.CSSProperties}>    <a className="navbar-brand" href="#top">Atlas</a>    <nav className="navbar-nav" aria-label="Main">      <a className="navbar-link" href="#product">Product</a>      <a className="navbar-link" href="#start" aria-current="page">Get started</a>      <a className="navbar-link" href="#pricing">Pricing</a>    </nav>    <div className="navbar-end">      <a className="btn btn-primary btn-sm" href="#sign-up">Sign up</a>      <button type="button" className="btn btn-quiet btn-square btn-sm navbar-toggle" popoverTarget="stack-menu" aria-label="Open the menu">        <MenuIcon className="icon" />      </button>      <div className="menu menu-end" id="stack-menu" popover="">        <a className="menu-item" href="#product">Product</a>        <a className="menu-item" href="#start" aria-current="page">Get started</a>        <a className="menu-item" href="#pricing">Pricing</a>      </div>    </div>  </header>  <main className="flex flex-col gap-2xl max-w(720) mx-auto px-lg py-2xl" id="stack-main" tabIndex={-1}>    <header className="flex flex-col gap-xs">      <p className="eyebrow">Get started</p>      <h1 className="heading heading-2xl">From a repository to a live site</h1>      <p className="lead">        Four steps, about five minutes. Each one is undone as easily as it is done.      </p>    </header>    <ol className="sticky-stack" style={{ '--sticky-stack-top': '4.5rem' } as React.CSSProperties}>      <li className="sticky-stack-item">        <article className="card min-h(256)">          <p className="eyebrow">Step 1</p>          <header className="card-header">            <h2 className="card-title">Connect a repository</h2>            <p className="card-description">Choose one you have, or start from a template.</p>          </header>          <div className="card-body well flex items-center justify-between gap-sm p-md">            <code className="code">atlas/site</code>            <span className="badge">main</span>          </div>          <footer className="card-footer">            <a className="btn btn-sm" href="#repositories">Choose another</a>          </footer>        </article>      </li>      <li className="sticky-stack-item">        <article className="card min-h(256)">          <p className="eyebrow">Step 2</p>          <header className="card-header">            <h2 className="card-title">Set the build</h2>            <p className="card-description">              The command that builds the site, and the folder it writes.            </p>          </header>          <div className="card-body well flex flex-col gap-3xs p-md">            <code className="code">bun run build</code>            <code className="code">dist/</code>          </div>          <footer className="card-footer">            <a className="btn btn-sm" href="#build">Edit the build</a>          </footer>        </article>      </li>      <li className="sticky-stack-item">        <article className="card min-h(256)">          <p className="eyebrow">Step 3</p>          <header className="card-header">            <h2 className="card-title">Preview every branch</h2>            <p className="card-description">              A pull request gets a link of its own, rebuilt on every push.            </p>          </header>          <div className="card-body well flex items-center justify-between gap-sm p-md">            <code className="code">pr-142.atlas.dev</code>            <span className="badge badge-success badge-dot">Ready</span>          </div>          <footer className="card-footer">            <a className="btn btn-sm" href="#preview">Open the preview</a>          </footer>        </article>      </li>      <li className="sticky-stack-item">        <article className="card min-h(256)">          <p className="eyebrow">Step 4</p>          <header className="card-header">            <h2 className="card-title">Ship</h2>            <p className="card-description">              Merge to main, and the preview you approved is what goes live.            </p>          </header>          <div className="card-body well flex items-center justify-between gap-sm p-md">            <code className="code">atlas.dev</code>            <span className="badge badge-success badge-live">Live</span>          </div>          <footer className="card-footer">            <a className="btn btn-sm btn-primary" href="#ship">Merge and ship</a>          </footer>        </article>      </li>    </ol>    <section className="flex flex-col gap-xs" aria-labelledby="stack-next">      <h2 className="heading heading-lg" id="stack-next">Then it is yours</h2>      <p className="m(0) leading-md">        Add a domain, invite the team, or leave it as it is. The stack has ended, so the steps scrolled away together, and the page goes on as a page.      </p>    </section>  </main></div>
<script>  import MenuIcon from '@master/icons-svelte/native/16/soft/menu'</script><div class="bg-surface-base fg-text-body text-sm">  <a class="skip" href="#stack-main">Skip to content</a>  <header class="navbar" style="--navbar-padding-x: max(1.5rem, calc(50% - 21rem))">    <a class="navbar-brand" href="#top">Atlas</a>    <nav class="navbar-nav" aria-label="Main">      <a class="navbar-link" href="#product">Product</a>      <a class="navbar-link" href="#start" aria-current="page">Get started</a>      <a class="navbar-link" href="#pricing">Pricing</a>    </nav>    <div class="navbar-end">      <a class="btn btn-primary btn-sm" href="#sign-up">Sign up</a>      <button type="button" class="btn btn-quiet btn-square btn-sm navbar-toggle" popovertarget="stack-menu" aria-label="Open the menu">        <MenuIcon class="icon" />      </button>      <div class="menu menu-end" id="stack-menu" popover>        <a class="menu-item" href="#product">Product</a>        <a class="menu-item" href="#start" aria-current="page">Get started</a>        <a class="menu-item" href="#pricing">Pricing</a>      </div>    </div>  </header>  <main class="flex flex-col gap-2xl max-w(720) mx-auto px-lg py-2xl" id="stack-main" tabindex="-1">    <header class="flex flex-col gap-xs">      <p class="eyebrow">Get started</p>      <h1 class="heading heading-2xl">From a repository to a live site</h1>      <p class="lead">        Four steps, about five minutes. Each one is undone as easily as it is done.      </p>    </header>    <ol class="sticky-stack" style="--sticky-stack-top: 4.5rem">      <li class="sticky-stack-item">        <article class="card min-h(256)">          <p class="eyebrow">Step 1</p>          <header class="card-header">            <h2 class="card-title">Connect a repository</h2>            <p class="card-description">Choose one you have, or start from a template.</p>          </header>          <div class="card-body well flex items-center justify-between gap-sm p-md">            <code class="code">atlas/site</code>            <span class="badge">main</span>          </div>          <footer class="card-footer">            <a class="btn btn-sm" href="#repositories">Choose another</a>          </footer>        </article>      </li>      <li class="sticky-stack-item">        <article class="card min-h(256)">          <p class="eyebrow">Step 2</p>          <header class="card-header">            <h2 class="card-title">Set the build</h2>            <p class="card-description">              The command that builds the site, and the folder it writes.            </p>          </header>          <div class="card-body well flex flex-col gap-3xs p-md">            <code class="code">bun run build</code>            <code class="code">dist/</code>          </div>          <footer class="card-footer">            <a class="btn btn-sm" href="#build">Edit the build</a>          </footer>        </article>      </li>      <li class="sticky-stack-item">        <article class="card min-h(256)">          <p class="eyebrow">Step 3</p>          <header class="card-header">            <h2 class="card-title">Preview every branch</h2>            <p class="card-description">              A pull request gets a link of its own, rebuilt on every push.            </p>          </header>          <div class="card-body well flex items-center justify-between gap-sm p-md">            <code class="code">pr-142.atlas.dev</code>            <span class="badge badge-success badge-dot">Ready</span>          </div>          <footer class="card-footer">            <a class="btn btn-sm" href="#preview">Open the preview</a>          </footer>        </article>      </li>      <li class="sticky-stack-item">        <article class="card min-h(256)">          <p class="eyebrow">Step 4</p>          <header class="card-header">            <h2 class="card-title">Ship</h2>            <p class="card-description">              Merge to main, and the preview you approved is what goes live.            </p>          </header>          <div class="card-body well flex items-center justify-between gap-sm p-md">            <code class="code">atlas.dev</code>            <span class="badge badge-success badge-live">Live</span>          </div>          <footer class="card-footer">            <a class="btn btn-sm btn-primary" href="#ship">Merge and ship</a>          </footer>        </article>      </li>    </ol>    <section class="flex flex-col gap-xs" aria-labelledby="stack-next">      <h2 class="heading heading-lg" id="stack-next">Then it is yours</h2>      <p class="m(0) leading-md">        Add a domain, invite the team, or leave it as it is. The stack has ended, so the steps scrolled away together, and the page goes on as a page.      </p>    </section>  </main></div>
<script setup>import MenuIcon from '@master/icons-vue/native/16/soft/menu'</script><template>  <div class="bg-surface-base fg-text-body text-sm">    <a class="skip" href="#stack-main">Skip to content</a>    <header class="navbar" style="--navbar-padding-x: max(1.5rem, calc(50% - 21rem))">      <a class="navbar-brand" href="#top">Atlas</a>      <nav class="navbar-nav" aria-label="Main">        <a class="navbar-link" href="#product">Product</a>        <a class="navbar-link" href="#start" aria-current="page">Get started</a>        <a class="navbar-link" href="#pricing">Pricing</a>      </nav>      <div class="navbar-end">        <a class="btn btn-primary btn-sm" href="#sign-up">Sign up</a>        <button type="button" class="btn btn-quiet btn-square btn-sm navbar-toggle" popovertarget="stack-menu" aria-label="Open the menu">          <MenuIcon class="icon" />        </button>        <div class="menu menu-end" id="stack-menu" popover>          <a class="menu-item" href="#product">Product</a>          <a class="menu-item" href="#start" aria-current="page">Get started</a>          <a class="menu-item" href="#pricing">Pricing</a>        </div>      </div>    </header>    <main class="flex flex-col gap-2xl max-w(720) mx-auto px-lg py-2xl" id="stack-main" tabindex="-1">      <header class="flex flex-col gap-xs">        <p class="eyebrow">Get started</p>        <h1 class="heading heading-2xl">From a repository to a live site</h1>        <p class="lead">          Four steps, about five minutes. Each one is undone as easily as it is done.        </p>      </header>      <ol class="sticky-stack" style="--sticky-stack-top: 4.5rem">        <li class="sticky-stack-item">          <article class="card min-h(256)">            <p class="eyebrow">Step 1</p>            <header class="card-header">              <h2 class="card-title">Connect a repository</h2>              <p class="card-description">Choose one you have, or start from a template.</p>            </header>            <div class="card-body well flex items-center justify-between gap-sm p-md">              <code class="code">atlas/site</code>              <span class="badge">main</span>            </div>            <footer class="card-footer">              <a class="btn btn-sm" href="#repositories">Choose another</a>            </footer>          </article>        </li>        <li class="sticky-stack-item">          <article class="card min-h(256)">            <p class="eyebrow">Step 2</p>            <header class="card-header">              <h2 class="card-title">Set the build</h2>              <p class="card-description">                The command that builds the site, and the folder it writes.              </p>            </header>            <div class="card-body well flex flex-col gap-3xs p-md">              <code class="code">bun run build</code>              <code class="code">dist/</code>            </div>            <footer class="card-footer">              <a class="btn btn-sm" href="#build">Edit the build</a>            </footer>          </article>        </li>        <li class="sticky-stack-item">          <article class="card min-h(256)">            <p class="eyebrow">Step 3</p>            <header class="card-header">              <h2 class="card-title">Preview every branch</h2>              <p class="card-description">                A pull request gets a link of its own, rebuilt on every push.              </p>            </header>            <div class="card-body well flex items-center justify-between gap-sm p-md">              <code class="code">pr-142.atlas.dev</code>              <span class="badge badge-success badge-dot">Ready</span>            </div>            <footer class="card-footer">              <a class="btn btn-sm" href="#preview">Open the preview</a>            </footer>          </article>        </li>        <li class="sticky-stack-item">          <article class="card min-h(256)">            <p class="eyebrow">Step 4</p>            <header class="card-header">              <h2 class="card-title">Ship</h2>              <p class="card-description">                Merge to main, and the preview you approved is what goes live.              </p>            </header>            <div class="card-body well flex items-center justify-between gap-sm p-md">              <code class="code">atlas.dev</code>              <span class="badge badge-success badge-live">Live</span>            </div>            <footer class="card-footer">              <a class="btn btn-sm btn-primary" href="#ship">Merge and ship</a>            </footer>          </article>        </li>      </ol>      <section class="flex flex-col gap-xs" aria-labelledby="stack-next">        <h2 class="heading heading-lg" id="stack-next">Then it is yours</h2>        <p class="m(0) leading-md">          Add a domain, invite the team, or leave it as it is. The stack has ended, so the steps scrolled away together, and the page goes on as a page.        </p>      </section>    </main>  </div></template>
點一下預覽後捲動。每個步驟都會停在導覽列下方,比前一個再低一點。選擇「手機」,即可在窄欄中看到相同的堆疊效果。

#結構

  • 每張面板都會黏附。 sticky-stack-item 使用 position: sticky。黏附盒會停在距最近捲動容器頂端 inset-block-start 的位置,此處的容器就是頁面。它只能在父元素,也就是堆疊的範圍內移動。因此,每個步驟都會停在導覽列下方,直到堆疊底部捲上來,四張面板才一起離開。
  • 每張都比前一張低一點。 面板的停靠位置以 --sticky-stack-top 為基準,前面每有一張面板,就加上一次 --sticky-stack-peek。樣式表用 sibling-index() 計算前面有幾張面板,因此無須在標記中編號。
  • 停在導覽列下方。 導覽列本身也會黏附,高度是 3.5rem。因此,堆疊將 --sticky-stack-top 設為 4.5rem,涵蓋導覽列高度並多留 1rem。
  • 用表面遮住下方的步驟。 每個項目都包含一張卡片,由卡片表面遮住下方的步驟。後面的面板在標記中出現得較晚,因此會繪製在前面的面板上方。
  • 用清單呈現步驟。 步驟順序具有意義,因此使用 <ol>。將卡片放在清單項目內,讓卡片維持自己的定位,由清單項目負責黏附。

#露出高度

--sticky-stack-peek 決定每張面板比前一張低多少,預設為 .75rem。露出的邊緣足以讓讀者數出目前面板後方還疊著幾個步驟。加大這個值,每張被蓋住的面板就會多露出一些,設為 2.5rem 時約可露出一個標題的高度。設為 0 時,所有面板都會停在同一條線上,完全蓋住前一張。

在不支援 sibling-index() 的瀏覽器中,所有面板都會停在同一條線上,效果如同設為 0。堆疊仍會運作,每個步驟捲到停靠位置時都會蓋住前一個步驟,只是邊緣不會露出。

#捲動容器

黏附面板以最近的捲動容器為基準,並且只能在父元素內移動。設定時請留意三點:

  • 項目必須是堆疊的直接子元素。如果在項目外再包一層元素,那一層就會成為它的父元素,使它沒有移動空間。項目內可放入任何內容,這裡放的是卡片。
  • 堆疊與捲動容器之間的元素都不能裁切堆疊。若在祖先元素上設定 overflow: hidden,該元素就會成為捲動容器,面板也就無法隨頁面捲動而黏附。
  • 面板會停留多久,取決於堆疊中排在它後面的內容。最後一張面板只會停留到堆疊底部捲到它的位置。若要讓它停久一點,可增加下方的堆疊高度,例如加入一張結尾面板。

#適用情境

當讀者需要隨頁面捲動依序閱讀簡短內容時,使用黏附堆疊。適合三到六個值得各自停留片刻的步驟、章節或方案。若面板需要並排顯示,或讓讀者自由切換,使用輪播。若是讓讀者一次展開一項的參考內容,使用折疊面板。清單過長時,二十張面板露出的邊緣會擠成一整片。

#無障礙

  • 堆疊只改變面板的繪製位置。輔助科技會依標記順序讀取,鍵盤焦點也會依相同順序移動。被遮住的面板內若有連結取得焦點,頁面會捲動到該面板進入檢視區為止。
  • 每張面板剛捲到停靠位置時都會完整顯示,讀者可在沒有其他面板遮擋的情況下閱讀。面板高度應小於檢視區高度減去 --sticky-stack-top 後的可用高度,太高的面板會在讀者看到末尾之前被蓋住。
  • 面板只會隨讀者捲動而移動,移動距離與捲動量一致。整個效果沒有動畫,因此無須另外處理減少動態效果的偏好。

#類別

類別說明
sticky-stack面板直向堆疊,間距為 1.5rem。容器本身不繪製任何內容,也不得裁切面板
sticky-stack-item面板會停駐在距捲動容器頂端 --sticky-stack-top 的位置。前面每多一個面板,停駐位置就往下移 --sticky-stack-peek,直到堆疊結束。

#自訂屬性

屬性預設值說明
--sticky-stack-top1rem第一個面板的停靠位置,以捲動容器頂端為基準,等於上方黏附式頁首的高度加上額外間距
--sticky-stack-peek.75rem每個面板比前一個面板低多少,也就是被覆蓋的面板仍露出的邊緣高度

#原始碼

@master/ui/layouts/sticky-stack.css
/* * Sticky stack: panels that pile up as the page scrolls. Each panel sticks when it reaches the top of the * scroller, a little lower than the one before it, so the edge of every panel it covers stays in view; when the * stack has scrolled to its end, they leave together. It is a layout: it decides what sticks where, and draws * nothing. The panels need a face of their own (a card, a sheet), or each would show the one under it. * * The step between two panels is --sticky-stack-peek, counted from each panel's place among its siblings with * sibling-index(). Without it every panel sticks on the same line and covers the one before it whole: the same * stack, without the edges. * * A sticky panel sticks to the nearest scroller and moves only inside its parent, so the panels are the * stack's own children and nothing between the stack and the scroller clips them (overflow: hidden would). * --sticky-stack-top is where the first one stops; under a sticky header, make it the header's height. * A sequence is a list, <ol class="sticky-stack">, so the stack clears a list's indent and markers. */@layer components.ui {  .sticky-stack {    --sticky-stack-top: 1rem;    --sticky-stack-peek: .75rem;    display: grid;    align-items: start;    gap: 1.5rem;    min-width: 0;    margin: 0;    padding: 0;    list-style: none;  }  .sticky-stack-item {    position: sticky;    inset-block-start: var(--sticky-stack-top);    min-width: 0;    @supports (inset-block-start: calc(sibling-index() * 1px)) {      inset-block-start: calc(var(--sticky-stack-top) + (sibling-index() - 1) * var(--sticky-stack-peek));    }  }}