網站導覽

v2.1.0
網站導覽

分頁導覽

以一列連結切換長清單的各個頁面,目前所在的頁面會呈現按下的凹陷狀態。

本頁內容

分頁導覽把長清單的各個頁面排成一列連結,放在 <nav> 中。連結是樣式低調的方形按鈕。目前頁面帶有 aria-current="page",外觀會維持凹下,像按下的切換按鈕。這份樣式表負責排列整列連結,也提供表示略過頁碼的省略標記。像指南章節這樣有名稱、需要依序閱讀的頁面,請用前後頁導覽。

分頁導覽

<nav aria-label="Pagination">  <ol class="pagination">    <li>      <a class="btn btn-quiet btn-square btn-sm" href="#page-1" aria-label="Previous page">        <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="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" transform="rotate(180 8 8)" />        </svg>      </a>    </li>    <li><a class="btn btn-quiet btn-square btn-sm" href="#page-1" aria-label="Page 1">1</a></li>    <li>      <a class="btn btn-quiet btn-square btn-sm" href="#page-2" aria-label="Page 2" aria-current="page">        2      </a>    </li>    <li><a class="btn btn-quiet btn-square btn-sm" href="#page-3" aria-label="Page 3">3</a></li>    <li class="pagination-gap" aria-hidden="true">…</li>    <li><a class="btn btn-quiet btn-square btn-sm" href="#page-24" aria-label="Page 24">24</a></li>    <li>      <a class="btn btn-quiet btn-square btn-sm" href="#page-3" aria-label="Next page">        <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="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" />        </svg>      </a>    </li>  </ol></nav>
import ChevronLeftIcon from '@master/icons-react/native/16/soft/chevron-left'import ChevronRightIcon from '@master/icons-react/native/16/soft/chevron-right'<nav aria-label="Pagination">  <ol className="pagination">    <li>      <a className="btn btn-quiet btn-square btn-sm" href="#page-1" aria-label="Previous page">        <ChevronLeftIcon className="icon" />      </a>    </li>    <li><a className="btn btn-quiet btn-square btn-sm" href="#page-1" aria-label="Page 1">1</a></li>    <li>      <a className="btn btn-quiet btn-square btn-sm" href="#page-2" aria-label="Page 2" aria-current="page">        2      </a>    </li>    <li><a className="btn btn-quiet btn-square btn-sm" href="#page-3" aria-label="Page 3">3</a></li>    <li className="pagination-gap" aria-hidden="true">…</li>    <li>      <a className="btn btn-quiet btn-square btn-sm" href="#page-24" aria-label="Page 24">24</a>    </li>    <li>      <a className="btn btn-quiet btn-square btn-sm" href="#page-3" aria-label="Next page">        <ChevronRightIcon className="icon" />      </a>    </li>  </ol></nav>
<script>  import ChevronLeftIcon from '@master/icons-svelte/native/16/soft/chevron-left'  import ChevronRightIcon from '@master/icons-svelte/native/16/soft/chevron-right'</script><nav aria-label="Pagination">  <ol class="pagination">    <li>      <a class="btn btn-quiet btn-square btn-sm" href="#page-1" aria-label="Previous page">        <ChevronLeftIcon class="icon" />      </a>    </li>    <li><a class="btn btn-quiet btn-square btn-sm" href="#page-1" aria-label="Page 1">1</a></li>    <li>      <a class="btn btn-quiet btn-square btn-sm" href="#page-2" aria-label="Page 2" aria-current="page">        2      </a>    </li>    <li><a class="btn btn-quiet btn-square btn-sm" href="#page-3" aria-label="Page 3">3</a></li>    <li class="pagination-gap" aria-hidden="true">…</li>    <li><a class="btn btn-quiet btn-square btn-sm" href="#page-24" aria-label="Page 24">24</a></li>    <li>      <a class="btn btn-quiet btn-square btn-sm" href="#page-3" aria-label="Next page">        <ChevronRightIcon class="icon" />      </a>    </li>  </ol></nav>
<script setup>import ChevronLeftIcon from '@master/icons-vue/native/16/soft/chevron-left'import ChevronRightIcon from '@master/icons-vue/native/16/soft/chevron-right'</script><template>  <nav aria-label="Pagination">    <ol class="pagination">      <li>        <a class="btn btn-quiet btn-square btn-sm" href="#page-1" aria-label="Previous page">          <ChevronLeftIcon class="icon" />        </a>      </li>      <li><a class="btn btn-quiet btn-square btn-sm" href="#page-1" aria-label="Page 1">1</a></li>      <li>        <a class="btn btn-quiet btn-square btn-sm" href="#page-2" aria-label="Page 2" aria-current="page">          2        </a>      </li>      <li><a class="btn btn-quiet btn-square btn-sm" href="#page-3" aria-label="Page 3">3</a></li>      <li class="pagination-gap" aria-hidden="true">…</li>      <li>        <a class="btn btn-quiet btn-square btn-sm" href="#page-24" aria-label="Page 24">24</a>      </li>      <li>        <a class="btn btn-quiet btn-square btn-sm" href="#page-3" aria-label="Next page">          <ChevronRightIcon class="icon" />        </a>      </li>    </ol>  </nav></template>

#只顯示上一頁和下一頁

總頁數未知或不重要時,兩個有標籤的按鈕就足夠了。連結本身無法停用,因此在第一頁請為「上一頁」加上 aria-disabled="true",並移除它的 href。

只顯示上一頁和下一頁

<nav class="flex items-center gap-xs" aria-label="Pagination">  <a class="btn btn-sm" aria-disabled="true">Previous</a>  <span class="text-sm fg-text-muted px-xs">Page 1 of 24</span>  <a class="btn btn-sm" href="#page-2">Next</a></nav>
<nav className="flex items-center gap-xs" aria-label="Pagination">  <a className="btn btn-sm" aria-disabled="true">Previous</a>  <span className="text-sm fg-text-muted px-xs">Page 1 of 24</span>  <a className="btn btn-sm" href="#page-2">Next</a></nav>
<nav class="flex items-center gap-xs" aria-label="Pagination">  <a class="btn btn-sm" aria-disabled="true">Previous</a>  <span class="text-sm fg-text-muted px-xs">Page 1 of 24</span>  <a class="btn btn-sm" href="#page-2">Next</a></nav>
<template>  <nav class="flex items-center gap-xs" aria-label="Pagination">    <a class="btn btn-sm" aria-disabled="true">Previous</a>    <span class="text-sm fg-text-muted px-xs">Page 1 of 24</span>    <a class="btn btn-sm" href="#page-2">Next</a>  </nav></template>

#無障礙

  • 用 <nav aria-label="Pagination"> 包住清單,並以 aria-current="page" 標記目前頁面。
  • 單獨的數字難以辨認連結用途。請為每個頁碼連結加上 aria-label,例如「第 3 頁」。
  • 省略標記無法操作,請用 aria-hidden="true" 將它從輔助技術中隱藏。

#類別

類別說明
pagination分頁連結列使用 ol,置於設有 aria-label 的 nav 中。頁碼採用等寬數字,切換頁面時整列寬度保持一致
pagination-gap用省略號表示略過的頁碼。省略號無法操作,請用 aria-hidden="true" 將它從無障礙樹中隱藏。

#原始程式碼

@master/ui/components/pagination.css
/* * Pagination: the pages of a long list, as a row of links inside a <nav>. * The links are quiet square buttons; the current page has aria-current="page" and stays sunk into a well, * which the button stylesheet draws. This file supplies the row and the gap that stands for skipped pages. */@layer components.ui {  .pagination {    display: flex;    flex-wrap: wrap;    align-items: center;    gap: 2px;    min-width: 0;    margin: 0;    padding: 0;    font-variant-numeric: tabular-nums;    list-style: none;    /* A button's font shorthand resets the figures, so the pages take the tabular ones on themselves. */    .btn {      font-variant-numeric: tabular-nums;    }    /*     * Pages stand 2px apart, so on touch a page's target grows only above and below, never over its neighbor;     * and pages that wrap onto a second row stand 12px apart there, the two reaches, for the same reason.     */    @media (pointer: coarse) {      row-gap: .75rem;      .btn::after {        @apply --touch-target-block;      }    }  }  /* An ellipsis where pages are left out. It is not a control, so hide it from assistive technology. */  .pagination-gap {    display: grid;    place-items: center;    min-width: 2rem;    height: 2rem;    color: var(--color-text-muted);    font-size: .78125rem;    user-select: none;    -webkit-user-select: none;  }}