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

微调器(Spinners)

使用 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构建的。这意味着可以轻松调整大小、重新着色和快速对齐它们。

边距

使用[边距实用程序][边距](如.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

变量

Added in v5.2.0

作为 Bootstrap 不断发展的 CSS 变量方法的一部分,微调器现在在.spinner-border.spinner-grow上使用本地 CSS 变量来增强实时自定义。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;

Keyframes

用于为我们的微调器创建 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;
  }
}