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 width="100%" height="180" class="card-img-top" text="false" background="#20c997" >}}
卡标题

一些基于卡片标题的快速示例文本构成了卡片的大部分内容。

¥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>
The use of aria-hidden="true" only indicates that the element should be hidden to screen readers. The loading behavior of the placeholder depends on how authors will actually use the placeholder styles, how they plan to update things, etc. Some JavaScript code may be needed to swap the state of the placeholder and inform AT users of the update.

宽度

¥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;