View on GitHub

旋转器(Spinners)

Bootstrap 的旋转器(spinners)用于指示组件或页面的加载状态,该组件是纯粹使用 HTML 和 CSS 实现的,没有任何 JavaScript 代码。

关于

Bootstrap 的旋转器(spinner)组件可用于显示项目的加载状态。该组件是纯粹使用 HTML 和 CSS 实现的,这意味着你无需使用任何 JavaScript 代码来创建它们。不过,你可能需要自己写一些 JavaScript 代码来切换该组件的可见性。该组件的外观、对齐和尺寸可以通过我们提供的工具类来轻松地自定义。

出于对无障碍(accessibility)的支持,这里的每个旋转器都设置了 role="status" 属性并嵌套了 <span class="sr-only">Loading...</span> 代码。

The animation effect of this component is dependent on the prefers-reduced-motion media query. See the reduced motion section of our accessibility documentation.

环状旋转器(Border spinner)

使用环状旋转器(border spinners)作为轻量级的加载指示器。

Loading...
<div class="spinner-border" role="status">
  <span class="sr-only">Loading...</span>
</div>

颜色

环状旋转器(border spinner)使用 currentColor 来设置其 border-color,这意味着你可以使用 文本颜色工具类 来自定义其颜色。你可以将任何文本颜色工具类应用到标准的旋转器(spinner)上。

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
<div class="spinner-border text-primary" role="status">
  <span class="sr-only">Loading...</span>
</div>
<div class="spinner-border text-secondary" role="status">
  <span class="sr-only">Loading...</span>
</div>
<div class="spinner-border text-success" role="status">
  <span class="sr-only">Loading...</span>
</div>
<div class="spinner-border text-danger" role="status">
  <span class="sr-only">Loading...</span>
</div>
<div class="spinner-border text-warning" role="status">
  <span class="sr-only">Loading...</span>
</div>
<div class="spinner-border text-info" role="status">
  <span class="sr-only">Loading...</span>
</div>
<div class="spinner-border text-light" role="status">
  <span class="sr-only">Loading...</span>
</div>
<div class="spinner-border text-dark" role="status">
  <span class="sr-only">Loading...</span>
</div>
为什么不使用 border-color 工具类呢? 因为每个环状旋转器( border spinner)至少为一个侧边设置了 transparent (透明)边框,而 .border-{color} 工具类将会覆盖该设置。

增长式旋转器(Growing spinner)

如果你不喜欢环状旋转器(border spinner),可以试试增长式旋转器( grow spinner)。虽然从技术来说它不是在旋转,但它是反复由小变大的!

Loading...
<div class="spinner-grow" role="status">
  <span class="sr-only">Loading...</span>
</div>

同样,这个旋转器也是使用 currentColor 来设置其 border-color 的,因此你可以使用 文本颜色工具类 轻松更改其外观。以下示例是一个蓝色的,还附上了其它可供选择的颜色。

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

对齐

Bootstrap 中的旋转器(Spinner)使用 rem 作为单位来设置其尺寸、使用currentColor 来定义其颜色、使用 display: inline-flex 设置其布局。这意味着我们可以轻松地调整其尺寸、颜色,并可以快速对齐。

外边距(margin)

可以使用类似 .m-5外边距(margin)工具类 来添加间距。

Loading...
<div class="spinner-border m-5" role="status">
  <span class="sr-only">Loading...</span>
</div>

位置

通过使用 flexbox 工具类浮动(float)工具类文本对齐工具类 可以在任何情况下将旋转器(spinner)放置到需要的位置上。

Flex

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

浮动(Floats)

Loading...
<div class="clearfix">
  <div class="spinner-border float-right" role="status">
    <span class="sr-only">Loading...</span>
  </div>
</div>

文本对齐

Loading...
<div class="text-center">
  <div class="spinner-border" role="status">
    <span class="sr-only">Loading...</span>
  </div>
</div>

尺寸

通过添加 .spinner-border-sm.spinner-grow-sm 类可以让旋转器(spinner)变得更小,以便在其它组件中使用。

Loading...
Loading...
<div class="spinner-border spinner-border-sm" role="status">
  <span class="sr-only">Loading...</span>
</div>
<div class="spinner-grow spinner-grow-sm" role="status">
  <span class="sr-only">Loading...</span>
</div>

或者,根据需要自己编写 CSS 或添加内联样式来更改旋转器(spinner)的尺寸。

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

按钮

在按钮中使用旋转器(spinners)来指示当前正在处理或正在进行的操作。还可以将旋转器(spinner)中包含的(提示性)文字去掉并使用按钮中包含的文字作为提示性信息。

<button class="btn btn-primary" type="button" disabled>
  <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
  <span class="sr-only">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>
<button class="btn btn-primary" type="button" disabled>
  <span class="spinner-grow spinner-grow-sm" role="status" aria-hidden="true"></span>
  <span class="sr-only">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>