导航

v2.1.0
反馈

骨架屏

用占位轮廓预示即将加载的内容,内容出现时页面不会跳动。

本页内容

骨架屏按即将加载的内容形状预留空间,避免内容出现时页面跳动。它是一个采用胶囊标签色调的色块,表面有一道柔和的光带掠过。在支持 sibling-index() 的浏览器中,同一区块里的骨架占位块依次启动动画,让光带从上到下连续扫过整个区块。让每个骨架占位块的尺寸与它所替代的内容一致。重新获取已有内容时,使用 忙碌状态。

骨架屏

<section class="flex items-center gap-sm" aria-busy="true" aria-label="Loading member">  <span class="skeleton skeleton-circle w(40)" aria-hidden="true"></span>  <div class="flex flex-col w-full text-sm" aria-hidden="true">    <span class="skeleton skeleton-text max-w(160)"></span>    <span class="skeleton skeleton-text max-w(320)"></span>  </div></section>
<section className="flex items-center gap-sm" aria-busy="true" aria-label="Loading member">  <span className="skeleton skeleton-circle w(40)" aria-hidden="true"></span>  <div className="flex flex-col w-full text-sm" aria-hidden="true">    <span className="skeleton skeleton-text max-w(160)"></span>    <span className="skeleton skeleton-text max-w(320)"></span>  </div></section>
<section class="flex items-center gap-sm" aria-busy="true" aria-label="Loading member">  <span class="skeleton skeleton-circle w(40)" aria-hidden="true"></span>  <div class="flex flex-col w-full text-sm" aria-hidden="true">    <span class="skeleton skeleton-text max-w(160)"></span>    <span class="skeleton skeleton-text max-w(320)"></span>  </div></section>
<template>  <section class="flex items-center gap-sm" aria-busy="true" aria-label="Loading member">    <span class="skeleton skeleton-circle w(40)" aria-hidden="true"></span>    <div class="flex flex-col w-full text-sm" aria-hidden="true">      <span class="skeleton skeleton-text max-w(160)"></span>      <span class="skeleton skeleton-text max-w(320)"></span>    </div>  </section></template>

#形状

  • 单独使用 skeleton 会显示为块状占位。将它的高度设为图片、图表或卡片的高度。
  • skeleton-text 用于占位一行文本。它与文字等高,在所占的行内垂直居中,让实际文本沿同一基线网格排版。它随所在位置的字号和行高变化。
  • skeleton-circle 用于头像或图标占位。设定宽度后,高度会随之调整。

加载中的卡片

<article class="card max-w(320) mx-auto" aria-busy="true" aria-label="Loading project">  <span class="skeleton card-media" aria-hidden="true"></span>  <div class="flex flex-col" aria-hidden="true">    <span class="skeleton skeleton-text max-w(160)"></span>    <span class="skeleton skeleton-text"></span>    <span class="skeleton skeleton-text max-w(240)"></span>  </div></article>
<article className="card max-w(320) mx-auto" aria-busy="true" aria-label="Loading project">  <span className="skeleton card-media" aria-hidden="true"></span>  <div className="flex flex-col" aria-hidden="true">    <span className="skeleton skeleton-text max-w(160)"></span>    <span className="skeleton skeleton-text"></span>    <span className="skeleton skeleton-text max-w(240)"></span>  </div></article>
<article class="card max-w(320) mx-auto" aria-busy="true" aria-label="Loading project">  <span class="skeleton card-media" aria-hidden="true"></span>  <div class="flex flex-col" aria-hidden="true">    <span class="skeleton skeleton-text max-w(160)"></span>    <span class="skeleton skeleton-text"></span>    <span class="skeleton skeleton-text max-w(240)"></span>  </div></article>
<template>  <article class="card max-w(320) mx-auto" aria-busy="true" aria-label="Loading project">    <span class="skeleton card-media" aria-hidden="true"></span>    <div class="flex flex-col" aria-hidden="true">      <span class="skeleton skeleton-text max-w(160)"></span>      <span class="skeleton skeleton-text"></span>      <span class="skeleton skeleton-text max-w(240)"></span>    </div>  </article></template>

#无障碍

  • 用 aria-hidden="true" 对辅助技术隐藏骨架占位块,在它所填充的区域上设置 aria-busy="true",并用标签说明正在加载的内容。
  • 启用减少动态效果时,光带保持静止,仅通过形状表示内容仍在加载。
  • 仅在用户可能察觉到等待时显示骨架屏。等待很短时无需显示提示。按钮使用 aria-busy 和 加载指示器。

#类名

类名说明
skeleton占位块,高度应与所替代的图片、图表或卡片一致
skeleton-text一条文本占位线,高度与字形相同,在所代表的文本行内垂直居中与 skeleton 一同使用
skeleton-circle用于头像或图标的圆形区域,大小由宽度决定与 skeleton 一同使用

#源码

@master/ui/components/skeleton.css
/* * Skeleton: the shape of content that is on its way. * A chip-toned block that a soft band of light crosses; with reduced motion it rests. * Hide it from assistive technology and mark the loading region with aria-busy. */@layer components.ui {  .skeleton {    display: block;    min-height: .75rem;    border-radius: calc(var(--radius-chip) * var(--corner-scale));    corner-shape: var(--corner-shape);    background:      linear-gradient(100deg, transparent 35%, var(--color-sheen) 50%, transparent 65%) 150% 0 / 300% 100% no-repeat,      var(--color-surface-chip);    color: transparent;    user-select: none;    -webkit-user-select: none;    pointer-events: none;    animation: skeleton-sweep 1.8s var(--easing-smooth) infinite;    /* The band crosses the way the words it stands for are read: from the right where they run from the right. */    &:dir(rtl) {      animation-direction: reverse;    }    /* One line of text: the block is as tall as the letters, centered in the line it stands for. */    &.skeleton-text {      height: .75em;      min-height: 0;      margin-block: calc((1lh - .75em) / 2);      border-radius: calc((var(--radius-chip) - 2px) * var(--corner-scale));    }    &.skeleton-circle {      aspect-ratio: 1;      border-radius: 999px;      corner-shape: round;    }    /* Lines in one block start one after another, so the band of light runs down the block as a single pass. */    @supports (width: calc(sibling-index() * 1px)) {      animation-delay: calc((sibling-index() - 1) * 90ms);    }    @media (prefers-reduced-motion: reduce) {      animation: none;    }    @media (forced-colors: active) {      outline: 1px solid GrayText;      outline-offset: -1px;    }  }}@keyframes skeleton-sweep {  to {    background-position: -50% 0, 0 0;  }}