导航

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() 统计前面的面板数量,HTML 标记中无需编号。
  • 停在栏下方。 这里的栏是一个导航栏,自身采用粘性定位,高度为 3.5rem。因此堆叠将 --sticky-stack-top 设为 4.5rem,即导航栏的高度再加 1rem 间距。
  • 用表面遮住下方的面板。 每个列表项都包含一张卡片,卡片的表面会遮住下方的面板。后面的面板在 HTML 标记中排得更靠后,因此绘制在先前面板的上层。
  • 用列表组织各个步骤。 步骤的顺序很重要,因此使用 <ol>。将卡片放在列表项内,由列表项执行粘性定位,卡片保持自身的位置。

#面板露出的边缘

--sticky-stack-peek 决定每个面板比前一个面板低多少,默认值为 .75rem。这个距离能让被遮住的面板各露出一点边缘,足以数清当前面板遮住了几个已经读过的步骤。值越大,露出的部分越多。设为 2.5rem 时,可以露出一个标题的高度。设为 0 时,所有面板都停在同一位置,每个面板都会完全遮住前一个。

浏览器不支持 sibling-index() 时,所有面板都停在同一位置,效果与设为 0 相同。堆叠仍然有效,每一步到达时都会盖住前一步,只是被盖住的面板边缘不会露出。

#滚动容器

粘性面板以最近的滚动容器为停靠参照,并且只能在其父元素内移动。由此有三点需要注意:

  • 列表项必须是堆叠容器的直接子元素。如果在列表项外再包一层元素,这一层就会成为它的父元素,列表项也就没有移动空间。列表项内可以放任意内容,此处放的是卡片。
  • 堆叠与滚动容器之间的元素都不能裁剪堆叠。在祖先元素上设置 overflow: hidden 会使该元素成为滚动容器,导致所有面板都无法停住。
  • 面板能停留多久,取决于堆叠中它后面的内容。最后一个面板只会停留到堆叠末尾滚到它所在的位置。要让它停留更久,就增加它下方的堆叠高度,例如加一个收尾面板。

#何时使用

需要让读者随页面滚动按顺序阅读少量内容时,可以使用粘性堆叠,例如三到六个值得逐一阅读的步骤、章节或方案。需要并排展示面板,或让读者自由切换面板时,使用轮播。对于读者按需逐项展开的参考资料,使用折叠面板。长列表不适合堆叠,二十个面板的边缘叠在一起会占据大量空间。

#无障碍

  • 堆叠只改变面板的绘制位置。阅读和键盘导航都遵循 HTML 标记的顺序。被遮住的面板中的链接获得焦点时,页面会滚动,直到该面板进入视野。
  • 每个面板出现时都完整可见,读者无需隔着上层面板阅读。面板高度应小于视口高度减去 --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));    }  }}