导航

v2.1.0
导航

跳过链接

页面中的首个链接,聚焦时出现,可跳过导航直达内容。

本页内容

跳过链接是页面上的第一个链接。它让键盘用户跳过导航,直接进入内容区,省去在每个页面上按 Tab 逐一经过页眉链接的操作。所有用户都能在文档中访问该链接,但它只在获得焦点时显示,单用指针操作时看不到它。

跳过链接

<a class="skip" href="#content">Skip to content</a><header class="navbar">  <a class="navbar-brand" href="#home">Atlas</a>  <nav class="navbar-nav" aria-label="Main">    <a class="navbar-link" href="#projects" aria-current="page">Projects</a>    <a class="navbar-link" href="#deployments">Deployments</a>    <a class="navbar-link" href="#settings">Settings</a>  </nav></header><main class="p-lg" id="content" tabindex="-1">  <h1 class="heading">Projects</h1>  <p class="lead lead-sm">Fourteen projects in three teams.</p></main>
<>  <a className="skip" href="#content">Skip to content</a>  <header className="navbar">    <a className="navbar-brand" href="#home">Atlas</a>    <nav className="navbar-nav" aria-label="Main">      <a className="navbar-link" href="#projects" aria-current="page">Projects</a>      <a className="navbar-link" href="#deployments">Deployments</a>      <a className="navbar-link" href="#settings">Settings</a>    </nav>  </header>  <main className="p-lg" id="content" tabIndex={-1}>    <h1 className="heading">Projects</h1>    <p className="lead lead-sm">Fourteen projects in three teams.</p>  </main></>
<a class="skip" href="#content">Skip to content</a><header class="navbar">  <a class="navbar-brand" href="#home">Atlas</a>  <nav class="navbar-nav" aria-label="Main">    <a class="navbar-link" href="#projects" aria-current="page">Projects</a>    <a class="navbar-link" href="#deployments">Deployments</a>    <a class="navbar-link" href="#settings">Settings</a>  </nav></header><main class="p-lg" id="content" tabindex="-1">  <h1 class="heading">Projects</h1>  <p class="lead lead-sm">Fourteen projects in three teams.</p></main>
<template>  <a class="skip" href="#content">Skip to content</a>  <header class="navbar">    <a class="navbar-brand" href="#home">Atlas</a>    <nav class="navbar-nav" aria-label="Main">      <a class="navbar-link" href="#projects" aria-current="page">Projects</a>      <a class="navbar-link" href="#deployments">Deployments</a>      <a class="navbar-link" href="#settings">Settings</a>    </nav>  </header>  <main class="p-lg" id="content" tabindex="-1">    <h1 class="heading">Projects</h1>    <p class="lead lead-sm">Fourteen projects in three teams.</p>  </main></template>
点击预览内部,然后按 Tab。

#标记

需满足以下两个条件:

  1. 链接位于页眉之前,是页面上第一个可聚焦的元素。它的 href 指向主内容区域的 id。
  2. 目标元素需设置 tabindex="-1"。片段链接在所有浏览器中都会将页面滚动到目标,但只有目标可以获得焦点时,焦点才会移过去。否则,再按一次 Tab,焦点就会回到刚跳过的页眉。

用链接文字说明它的作用,例如“跳到正文”。如果页面的侧栏很长,可以在第一个链接之后加上“跳到导航”。

#显示方式

未聚焦时,链接位于视口上边缘之外,完全透明,无法按下,但仍保留在 Tab 顺序中。设为 display: none 的元素本身会被跳过。获得焦点后,链接以墨色小控件的形式从视口顶角向下滑入,停在距顶部 8px 的位置,显示在粘性 导航栏 和页面上的其他内容之上。--skip-layer 设置它的 z-index。

跳过链接需要出现在视口角落,因此是少数自行定位的组件之一。在有刘海的手机上,它会留在安全区内,避开刘海遮挡。

#无障碍

  • 凡是在正文前放置导航的页面,都需要一个跳过链接。WCAG 2.4.1“绕过区块”要求提供跳过各页面重复内容的方式,跳过链接是常见的实现方式。
  • 链接在 :focus 时显示,涵盖 :focus-visible 之外的聚焦情形,因此使用开关设备或屏幕阅读器自带导航功能的用户到达链接时,也能看到它。
  • 启用减少动态效果时,链接直接在最终位置显示,省去滑入动画。
  • 如果页面上有多个类似 <main> 的区域,请用标题或 aria-label 为目标区域命名,让用户跳转后知道自己到了哪里。

#类名

类名说明
skip用于指向 id 的锚点链接,以小型墨色控件的形式固定在顶部一角内侧 8px 处,聚焦后才显示。长标签在视口内换行。

#自定义属性

属性默认值说明
--skip-layer1000使元素位于粘性导航栏上方的 z-index。

#源码

@master/ui/components/skip.css
/* * Skip: the first link of a page, which jumps past the navigation to the content. * It is in the document for everyone and on the screen only while it has focus: a reader who starts with the * Tab key meets it first, and nobody else sees it. Focused, it drops in at the top corner of the viewport as a * small control of ink, over everything else on the page. * It stands there because standing there is its job: fixed, 8px from the corner, clear of a notch. * Geometry: 38px tall with a control's corner, as a button is. */@layer components.ui {  .skip {    --skip-layer: 1000;    position: fixed;    z-index: var(--skip-layer);    inset-block-start: max(.5rem, env(safe-area-inset-top));    inset-inline-start: max(.5rem, env(safe-area-inset-left));    display: inline-flex;    align-items: center;    box-sizing: border-box;    min-height: 2.375rem;    padding: 0 1rem;    border-radius: calc(var(--radius-control) * var(--corner-scale));    corner-shape: var(--corner-shape);    @apply --control-ink;    color: var(--color-text-inverse);    font: var(--font-weight-control) .84375rem/1.2 var(--font-family-sans);    font-feature-settings: inherit;    text-decoration: none;    /*     * A long label wraps inside the viewport instead of leaving it: the width of the viewport less its scrollbar     * (a percentage of a fixed box's viewport, where 100vw would count the scrollbar too) and both insets.     */    max-width: calc(100% - max(.5rem, env(safe-area-inset-left)) - max(.5rem, env(safe-area-inset-right)));    padding-block: .5rem;    text-wrap: balance;    /* At rest it is above the viewport and cannot be pressed; it keeps its place in the tab order. */    opacity: 0;    translate: 0 calc(-100% - 1rem);    pointer-events: none;    transition: translate var(--duration-quick) var(--easing-glide), opacity var(--duration-quick) var(--easing-glide);    &:focus {      opacity: 1;      translate: 0 0;      pointer-events: auto;    }    &:focus-visible {      @apply --focus-ring;    }    /* Shown, it answers as the primary button does: a lighter face under the pointer, the well under a press. */    @media (hover: hover) {      &:focus:hover {        @apply --control-ink-hover;      }    }    &:focus:active {      @apply --control-ink-pressed;    }    /* Where words run from the right, the start corner is the right one, clear of the notch on that side. */    &:dir(rtl) {      inset-inline-start: max(.5rem, env(safe-area-inset-right));    }    @media (pointer: coarse) {      &::after {        @apply --touch-target;      }    }    /* With reduced motion it appears where it stands, without the drop, and still fades in and out. */    @media (prefers-reduced-motion: reduce) {      translate: none;      transition-property: opacity;    }    @media (forced-colors: active) {      outline: 1px solid ButtonText;      outline-offset: -1px;    }  }}