Skip to main content Skip to docs navigation
Added in v5.1

占位符

在你的组件或页面中使用加载占位符(骨架加载器),以指示某些内容可能仍在加载中。

关于

🌐 About

占位符可以用来增强你的应用体验。它们仅由 HTML 和 CSS 构建,这意味着你无需任何 JavaScript 就可以创建它们。然而,你需要一些自定义的 JavaScript 来切换它们的可见性。它们的外观、颜色和大小可以通过我们的实用类轻松自定义。

🌐 Placeholders can be used to enhance the experience of your application. They’re built only with HTML and CSS, meaning you don’t need any JavaScript to create them. You will, however, need some custom JavaScript to toggle their visibility. Their appearance, color, and sizing can be easily customized with our utility classes.

示例

🌐 Example

在下面的示例中,我们采用一个典型的卡片组件,并用占位符重新创建它,以生成一个“加载中的卡片”。两者的大小和比例相同。

🌐 In the example below, we take a typical card component and recreate it with placeholders applied to create a “loading card”. Size and proportions are the same between the two.

Placeholder
Card title

Some quick example text to build on the card title and make up the bulk of the card’s content.

Go somewhere
<div class="card">
  <img src="..." class="card-img-top" alt="...">

  <div class="card-body">
    <h5 class="card-title">Card title</h5>
    <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card’s content.</p>
    <a href="#" class="btn btn-primary">Go somewhere</a>
  </div>
</div>

<div class="card" aria-hidden="true">
  <img src="..." class="card-img-top" alt="...">
  <div class="card-body">
    <h5 class="card-title placeholder-glow">
      <span class="placeholder col-6"></span>
    </h5>
    <p class="card-text placeholder-glow">
      <span class="placeholder col-7"></span>
      <span class="placeholder col-4"></span>
      <span class="placeholder col-4"></span>
      <span class="placeholder col-6"></span>
      <span class="placeholder col-8"></span>
    </p>
    <a class="btn btn-primary disabled placeholder col-6" aria-disabled="true"></a>
  </div>
</div>

怎么运行的

🌐 How it works

使用 .placeholder 类和网格列类(例如,.col-6)创建占位符以设置 width。它们可以替换元素内的文本,或作为修饰类添加到现有组件中。

🌐 Create placeholders with the .placeholder class and a grid column class (e.g., .col-6) to set the width. They can replace the text inside an element or be added as a modifier class to an existing component.

我们通过 ::before.btn 应用额外的样式,以确保 height 得到遵守。你可以根据需要将此模式扩展到其他情况,或者在元素中添加 &nbsp; 来反映实际文本渲染时的高度。

🌐 We apply additional styling to .btns via ::before to ensure the height is respected. You may extend this pattern for other situations as needed, or add a &nbsp; within the element to reflect the height when actual text is rendered in its place.

html
<p aria-hidden="true">
  <span class="placeholder col-6"></span>
</p>

<a class="btn btn-primary disabled placeholder col-4" aria-disabled="true"></a>

使用 aria-hidden="true" 仅表示该元素应对屏幕阅读器隐藏。占位符的 加载 行为取决于作者实际上如何使用占位符样式、他们计划如何更新内容等。可能需要一些 JavaScript 代码来 切换 占位符的状态并通知辅助技术(AT)用户更新情况。

宽度

🌐 Width

你可以通过网格列类、宽度工具类或内联样式来更改 width

🌐 You can change the width through grid column classes, width utilities, or inline styles.

html
<span class="placeholder col-6"></span>
<span class="placeholder w-75"></span>
<span class="placeholder" style="width: 25%;"></span>

颜色

🌐 Color

默认情况下,placeholder 使用 currentColor。可以通过自定义颜色或工具类来覆盖此设置。

🌐 By default, the placeholder uses currentColor. This can be overridden with a custom color or utility class.

html
<span class="placeholder col-12"></span>

<span class="placeholder col-12 bg-primary"></span>
<span class="placeholder col-12 bg-secondary"></span>
<span class="placeholder col-12 bg-success"></span>
<span class="placeholder col-12 bg-danger"></span>
<span class="placeholder col-12 bg-warning"></span>
<span class="placeholder col-12 bg-info"></span>
<span class="placeholder col-12 bg-light"></span>
<span class="placeholder col-12 bg-dark"></span>

尺寸

🌐 Sizing

.placeholder 的大小是基于父元素的排版样式。可以使用尺寸修饰符自定义它们:.placeholder-lg.placeholder-sm.placeholder-xs

🌐 The size of .placeholders are based on the typographic style of the parent element. Customize them with sizing modifiers: .placeholder-lg, .placeholder-sm, or .placeholder-xs.

html
<span class="placeholder col-12 placeholder-lg"></span>
<span class="placeholder col-12"></span>
<span class="placeholder col-12 placeholder-sm"></span>
<span class="placeholder col-12 placeholder-xs"></span>

动画

🌐 Animation

使用 .placeholder-glow.placeholder-wave 动画化占位符,以更好地传达某物正在主动加载的感知。

🌐 Animate placeholders with .placeholder-glow or .placeholder-wave to better convey the perception of something being actively loaded.

html
<p class="placeholder-glow">
  <span class="placeholder col-12"></span>
</p>

<p class="placeholder-wave">
  <span class="placeholder col-12"></span>
</p>

CSS

Sass 变量

🌐 Sass variables

$placeholder-opacity-max:           .5;
$placeholder-opacity-min:           .2;