导航

v2.1.0
导航

前后页导航

在文章末尾显示前一页和后一页。

本页内容

前后页导航位于文章底部,由两个并排的链接组成。每个链接都是平坦的表面,目标页标题上方有一行低调的标签。链接用 rel="prev" 和 rel="next" 标明方向,样式表据此应用样式,无需为方向单独添加类名。

前后页导航

<nav class="pager max-w(560) mx-auto" aria-label="Guide pages">  <a class="pager-link" rel="prev" href="#installation">    <span class="pager-label">      <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="M13.6362 7.5189A0.4947 0.4947 0 0 0 13.5 7.5L2.4755 7.5006A0.5 0.5 0 0 0 2.4266 8.4946L2.5 8.5H13.5A0.4947 0.4947 0 0 0 13.6362 8.4811A0.7009 0.7009 0 0 0 13.6362 7.5189Z" fill="currentColor" stroke="none" fill-rule="evenodd" transform="rotate(180 8 8)" /><path d="M8 4.5L13.2348 7.8313A0.2 0.2 0 0 1 13.2348 8.1687L8 11.5" transform="rotate(180 8 8)" />      </svg>Previous    </span>    <span class="pager-title">Installation</span>  </a>  <a class="pager-link" rel="next" href="#theming">    <span class="pager-label">      Next<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="M13.6362 7.5189A0.4947 0.4947 0 0 0 13.5 7.5L2.4755 7.5006A0.5 0.5 0 0 0 2.4266 8.4946L2.5 8.5H13.5A0.4947 0.4947 0 0 0 13.6362 8.4811A0.7009 0.7009 0 0 0 13.6362 7.5189Z" fill="currentColor" stroke="none" fill-rule="evenodd" /><path d="M8 4.5L13.2348 7.8313A0.2 0.2 0 0 1 13.2348 8.1687L8 11.5" />      </svg>    </span>    <span class="pager-title">Theming</span>  </a></nav>
import ArrowLeftIcon from '@master/icons-react/native/16/soft/arrow-left'import ArrowRightIcon from '@master/icons-react/native/16/soft/arrow-right'<nav className="pager max-w(560) mx-auto" aria-label="Guide pages">  <a className="pager-link" rel="prev" href="#installation">    <span className="pager-label"><ArrowLeftIcon className="icon" />Previous</span>    <span className="pager-title">Installation</span>  </a>  <a className="pager-link" rel="next" href="#theming">    <span className="pager-label">Next<ArrowRightIcon className="icon" /></span>    <span className="pager-title">Theming</span>  </a></nav>
<script>  import ArrowLeftIcon from '@master/icons-svelte/native/16/soft/arrow-left'  import ArrowRightIcon from '@master/icons-svelte/native/16/soft/arrow-right'</script><nav class="pager max-w(560) mx-auto" aria-label="Guide pages">  <a class="pager-link" rel="prev" href="#installation">    <span class="pager-label"><ArrowLeftIcon class="icon" />Previous</span>    <span class="pager-title">Installation</span>  </a>  <a class="pager-link" rel="next" href="#theming">    <span class="pager-label">Next<ArrowRightIcon class="icon" /></span>    <span class="pager-title">Theming</span>  </a></nav>
<script setup>import ArrowLeftIcon from '@master/icons-vue/native/16/soft/arrow-left'import ArrowRightIcon from '@master/icons-vue/native/16/soft/arrow-right'</script><template>  <nav class="pager max-w(560) mx-auto" aria-label="Guide pages">    <a class="pager-link" rel="prev" href="#installation">      <span class="pager-label"><ArrowLeftIcon class="icon" />Previous</span>      <span class="pager-title">Installation</span>    </a>    <a class="pager-link" rel="next" href="#theming">      <span class="pager-label">Next<ArrowRightIcon class="icon" /></span>      <span class="pager-title">Theming</span>    </a>  </nav></template>

#一个属性决定链接位置

rel 是 HTML 属性,用于说明链接与当前页面的关系,prev 和 next 是它的两个取值。前后页导航中的每个链接都使用其中一个值,正确表达页面的先后关系。样式表读取该属性,实现以下三点。

  • 下一页链接位于行末,文字也向行末对齐。
  • 指针悬停时,箭头沿所指方向移动:下一页向前,上一页向后。在从右向左的页面中,两个箭头都反向。
  • 只有下一页链接时,它仍位于行末,方便读者看到并用拇指点按。

箭头是标签里的图标,使用 16px 的 Master Icons 图稿,由标签以 14px 显示:上一页在文字前放 arrow-left,下一页在文字后放 arrow-right。

省略没有对应页面的链接。指南的第一页只显示下一页,最后一页只显示上一页。

第一页

<nav class="pager max-w(560) mx-auto" aria-label="Guide pages">  <a class="pager-link" rel="next" href="#installation">    <span class="pager-label">      Next<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="M13.6362 7.5189A0.4947 0.4947 0 0 0 13.5 7.5L2.4755 7.5006A0.5 0.5 0 0 0 2.4266 8.4946L2.5 8.5H13.5A0.4947 0.4947 0 0 0 13.6362 8.4811A0.7009 0.7009 0 0 0 13.6362 7.5189Z" fill="currentColor" stroke="none" fill-rule="evenodd" /><path d="M8 4.5L13.2348 7.8313A0.2 0.2 0 0 1 13.2348 8.1687L8 11.5" />      </svg>    </span>    <span class="pager-title">Installation</span>  </a></nav>
import ArrowRightIcon from '@master/icons-react/native/16/soft/arrow-right'<nav className="pager max-w(560) mx-auto" aria-label="Guide pages">  <a className="pager-link" rel="next" href="#installation">    <span className="pager-label">Next<ArrowRightIcon className="icon" /></span>    <span className="pager-title">Installation</span>  </a></nav>
<script>  import ArrowRightIcon from '@master/icons-svelte/native/16/soft/arrow-right'</script><nav class="pager max-w(560) mx-auto" aria-label="Guide pages">  <a class="pager-link" rel="next" href="#installation">    <span class="pager-label">Next<ArrowRightIcon class="icon" /></span>    <span class="pager-title">Installation</span>  </a></nav>
<script setup>import ArrowRightIcon from '@master/icons-vue/native/16/soft/arrow-right'</script><template>  <nav class="pager max-w(560) mx-auto" aria-label="Guide pages">    <a class="pager-link" rel="next" href="#installation">      <span class="pager-label">Next<ArrowRightIcon class="icon" /></span>      <span class="pager-title">Installation</span>    </a>  </nav></template>

#材质与动效

链接采用一道轮廓围合的平坦表面,圆角与大号控件一致。指针悬停时,链接抬高一层,箭头沿指向移动 2px。按下时,链接沉入凹槽。启用减少动态效果时,箭头保持原位。

前后页导航是一个尺寸容器。当自身宽度小于 26rem 时,两个链接上下排列,各自占满导航宽度,上一页排在最前。

#前后页导航与分页

前后页导航用于在按顺序阅读的页面之间切换,并显示目标页的标题。分页 用于在长列表的各个编号页之间切换。面包屑导航 显示当前页面在网站中的位置。

#无障碍

  • 将链接放入带有 aria-label 的 <nav> 中,让这个导航地标能与页面上的其他导航区分开。
  • 链接的无障碍名称由标签和标题共同组成,例如“下一页,主题设置”。保留标签,用它说明链接指向上一页还是下一页。
  • 箭头是带有 aria-hidden="true" 的图标,读屏软件不会读出它们。

#类名

类名说明
pager两个链接在带无障碍名称的 nav 内并排显示,容器宽度小于 26rem 时改为纵向排列。
pager-link一个带 rel="prev" 或 rel="next" 的链接:表面平坦,指针悬停时抬高一级。下一页链接位于末端,文字也向末端对齐
pager-label标题上方弱化显示的“上一页”或“下一页”标签,并带有指示翻页方向的图标:上一页的箭头在文字之前,下一页的箭头在文字之后。标签将图标设为 14px,并在从右向左的页面中将它反向
pager-title链接目标页面的标题

#源码

@master/ui/components/pager.css
/* * Pager: the page before and the page after, at the foot of an article. * Two links side by side, each a flat face with a quiet word over the title of the page it leads to. The * links say which is which with rel="prev" and rel="next", and the stylesheet reads that: the next page * stands at the end with its words set to the end, and an arrow points the way each one goes. The arrow is an * icon in the markup, beside the word: the stylesheet sizes it, moves it and turns it in a right-to-left page. * A pager with only a next page keeps it at the end. * Under the pointer a link rises a step and its arrow moves 2px the way it points. In a narrow container the * two stack. * Geometry: a large control's corner, 14px by 16px of padding, 12px between the two. */@layer components.ui {  .pager {    container-type: inline-size;    display: grid;    grid-template-columns: repeat(2, minmax(0, 1fr));    gap: .75rem;    min-width: 0;    margin: 0;    padding: 0;    list-style: none;    @container (width < 26rem) {      .pager-link, .pager-link[rel~="next"] {        grid-column: 1 / -1;      }    }  }  .pager-link {    --pager-travel: 0px;    display: grid;    gap: .125rem;    min-width: 0;    padding: .875rem 1rem;    border-radius: calc(var(--radius-control-lg) * var(--corner-scale));    corner-shape: var(--corner-shape);    background: var(--color-surface-raised);    color: var(--color-text-body);    box-shadow: var(--shadow-flat);    text-decoration: none;    touch-action: manipulation;    -webkit-tap-highlight-color: transparent;    transition: box-shadow var(--duration-quick) var(--easing-glide);    /* The page after: at the end, with its words set to the end. */    &[rel~="next"] {      grid-column: 2;      justify-items: end;      text-align: end;    }    @media (hover: hover) {      &:hover {        --pager-travel: 2px;        box-shadow: var(--shadow-raised);      }    }    &:active {      background: var(--color-surface-inset);      box-shadow: var(--shadow-pressed);      transition-duration: var(--duration-press);    }    &:focus-visible {      @apply --focus-ring;    }    @media (prefers-reduced-motion: reduce) {      --pager-travel: 0px;      @media (hover: hover) {        &:hover {          --pager-travel: 0px;        }      }    }    @media (forced-colors: active) {      outline: 1px solid LinkText;      outline-offset: -1px;    }  }  /*   * The quiet word: Previous, Next. The icon beside it is its arrow, 14px, which the label holds before the word   * of the page before and after the word of the next.   */  .pager-label {    --icon-size: .875rem;    display: inline-flex;    align-items: center;    gap: .25rem;    margin: 0;    color: var(--color-text-muted);    font-size: .78125rem;    line-height: 1.25rem;    .icon {      transition: translate var(--duration-quick) var(--easing-glide);    }  }  /* Under the pointer an arrow moves the way it points: back for the page before, on for the next. */  .pager-link:not([rel~="next"]) .pager-label .icon {    translate: calc(-1 * var(--pager-travel)) 0;  }  .pager-link[rel~="next"] .pager-label .icon {    translate: var(--pager-travel) 0;  }  /* In a right-to-left page the same two drawings are turned round, and so is the way each moves. */  .pager-link:not([rel~="next"]) .pager-label:dir(rtl) .icon {    scale: -1 1;    translate: var(--pager-travel) 0;  }  .pager-link[rel~="next"] .pager-label:dir(rtl) .icon {    scale: -1 1;    translate: calc(-1 * var(--pager-travel)) 0;  }  /* The title of the page it leads to. */  .pager-title {    min-width: 0;    margin: 0;    color: var(--color-text-strong);    font-weight: var(--font-weight-control);    font-size: .9375rem;    line-height: 1.35;    text-wrap: balance;    overflow-wrap: anywhere;  }}