跳到主要内容 跳到文档导航

加载旋转指示器

使用 Bootstrap 加载旋转指示器指示组件或页面的加载状态,完全使用 HTML、CSS 构建,没有 JavaScript。

关于

Bootstrap“加载旋转指示器”可用于显示项目中的加载状态。它们仅使用 HTML 和 CSS 构建,这意味着您不需要任何 JavaScript 来创建它们。但是,您将需要一些自定义 JavaScript 来切换它们的可见性。它们的外观、对齐方式和尺寸可以通过我们惊人的实用程序类轻松定制。

出于可访问性目的,这里的每个加载器都包含role="status"和一个嵌套的<span class="visually-hidden">Loading...</span>.

该组件的动画效果依赖于prefers-reduced-motion媒体查询。请参阅我们的辅助功能文档的减少运动部分

边框加载旋转指示器

使用边框加载旋转指示器作为轻量级加载指示器。

Loading...
HTML
<div class="spinner-border" role="status">
  <span class="visually-hidden">Loading...</span>
</div>

颜色

边框加载旋转指示器使用currentColor用于border-color,这意味着您可以使用文本颜色实用程序自定义颜色。您可以在标准加载旋转指示器上使用我们的任何文本颜色实用程序。

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
HTML
<div class="spinner-border text-primary" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner-border text-secondary" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner-border text-success" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner-border text-danger" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner-border text-warning" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner-border text-info" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner-border text-light" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner-border text-dark" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
为什么不使用border-color实用程序?每个边框加载旋转指示器transparent至少为一侧指定一个边框,因此.border-{color}实用程序会覆盖它。

增长指示器

如果您不喜欢边框加载旋转指示器,请切换到增长加载旋转指示器。虽然从技术上讲它不会旋转,但它会反复生长!

Loading...
HTML
<div class="spinner-grow" role="status">
  <span class="visually-hidden">Loading...</span>
</div>

再一次,这个加载旋转指示器是用构建的,因此您可以使用文本颜色实用程序currentColor轻松更改它的外观。这里是蓝色的,还有支持的变体。

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
HTML
<div class="spinner-grow text-primary" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner-grow text-secondary" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner-grow text-success" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner-grow text-danger" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner-grow text-warning" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner-grow text-info" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner-grow text-light" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner-grow text-dark" role="status">
  <span class="visually-hidden">Loading...</span>
</div>

对齐

Bootstrap 中的加载旋转指示器是用remcurrentColordisplay: inline-flex构建的。这意味着它们可以轻松调整大小、重新着色和快速对齐。

边距

使用margin 实用程序来简化.m-5间距。

Loading...
HTML
<div class="spinner-border m-5" role="status">
  <span class="visually-hidden">Loading...</span>
</div>

位置

在任何情况下,使用flexbox 实用程序浮动实用程序文本对齐实用程序将加载旋转指示器准确位置在您需要的位置。

Flex

Loading...
HTML
<div class="d-flex justify-content-center">
  <div class="spinner-border" role="status">
    <span class="visually-hidden">Loading...</span>
  </div>
</div>
Loading...
HTML
<div class="d-flex align-items-center">
  <strong>Loading...</strong>
  <div class="spinner-border ms-auto" role="status" aria-hidden="true"></div>
</div>

浮动

Loading...
HTML
<div class="clearfix">
  <div class="spinner-border float-end" role="status">
    <span class="visually-hidden">Loading...</span>
  </div>
</div>

文本对齐

Loading...
HTML
<div class="text-center">
  <div class="spinner-border" role="status">
    <span class="visually-hidden">Loading...</span>
  </div>
</div>

尺寸

添加.spinner-border-sm.spinner-grow-sm制作一个可以在其他组件中快速使用的较小的加载旋转指示器。

Loading...
Loading...
HTML
<div class="spinner-border spinner-border-sm" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner-grow spinner-grow-sm" role="status">
  <span class="visually-hidden">Loading...</span>
</div>

或者,使用自定义 CSS 或内联样式根据需要更改尺寸。

Loading...
Loading...
HTML
<div class="spinner-border" style="width: 3rem; height: 3rem;" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner-grow" style="width: 3rem; height: 3rem;" role="status">
  <span class="visually-hidden">Loading...</span>
</div>

按钮

在按钮内使用加载旋转指示器来指示当前正在处理或正在进行的操作。您还可以将文本从加载旋转指示器元素中换出,并根据需要使用按钮文本。

HTML
<button class="btn btn-primary" type="button" disabled>
  <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
  <span class="visually-hidden">Loading...</span>
</button>
<button class="btn btn-primary" type="button" disabled>
  <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
  Loading...
</button>
HTML
<button class="btn btn-primary" type="button" disabled>
  <span class="spinner-grow spinner-grow-sm" role="status" aria-hidden="true"></span>
  <span class="visually-hidden">Loading...</span>
</button>
<button class="btn btn-primary" type="button" disabled>
  <span class="spinner-grow spinner-grow-sm" role="status" aria-hidden="true"></span>
  Loading...
</button>

CSS

变量

添加于 v5.2.0

作为 Bootstrap 不断发展的 CSS 变量方法的一部分,加载旋转指示器现在使用本地 CSS 变量.spinner-border.spinner-grow增强实时定制。CSS 变量的值是通过 Sass 设置的,因此仍然支持 Sass 自定义。

边框加载旋转指示器变量:

  --#{$prefix}spinner-width: #{$spinner-width};
  --#{$prefix}spinner-height: #{$spinner-height};
  --#{$prefix}spinner-vertical-align: #{$spinner-vertical-align};
  --#{$prefix}spinner-border-width: #{$spinner-border-width};
  --#{$prefix}spinner-animation-speed: #{$spinner-animation-speed};
  --#{$prefix}spinner-animation-name: spinner-border;
  

增加加载旋转指示器变量:

  --#{$prefix}spinner-width: #{$spinner-width};
  --#{$prefix}spinner-height: #{$spinner-height};
  --#{$prefix}spinner-vertical-align: #{$spinner-vertical-align};
  --#{$prefix}spinner-animation-speed: #{$spinner-animation-speed};
  --#{$prefix}spinner-animation-name: spinner-grow;
  

对于这两个加载旋转指示器,小型加载旋转指示器修饰符类用于根据需要更新这些 CSS 变量的值。例如,.spinner-border-sm类执行以下操作:

  --#{$prefix}spinner-width: #{$spinner-width-sm};
  --#{$prefix}spinner-height: #{$spinner-height-sm};
  --#{$prefix}spinner-border-width: #{$spinner-border-width-sm};
  

Sass 变量

$spinner-width:           2rem;
$spinner-height:          $spinner-width;
$spinner-vertical-align:  -.125em;
$spinner-border-width:    .25em;
$spinner-animation-speed: .75s;

$spinner-width-sm:        1rem;
$spinner-height-sm:       $spinner-width-sm;
$spinner-border-width-sm: .2em;

关键帧

用于为我们的加载旋转指示器创建 CSS 动画。包含在scss/_spinners.scss.

@keyframes spinner-border {
  to { transform: rotate(360deg) #{"/* rtl:ignore */"}; }
}
@keyframes spinner-grow {
  0% {
    transform: scale(0);
  }
  50% {
    opacity: 1;
    transform: none;
  }
}