Skip to main content Skip to docs navigation

旋转特效(Spinners)

使用 Bootstrap 旋转器来显示组件或页面的加载状态,旋转器完全由 HTML、CSS 和 JavaScript 构建,无需 JavaScript。

关于

Bootstrap 的 “旋转器” 可用于显示项目中的加载状态。它们仅使用 HTML 和 CSS 构建,这意味着您不需要任何 JavaScript 就能创建它们。不过,您需要一些自定义 JavaScript 来切换它们的可见性。它们的外观、对齐方式和大小可以通过我们出色的实用工具类轻松定制。

为便于访问,这里的每个加载器都包含 role="status" 和嵌套的 <span class="visually-hidden">加载...</span>

该组件的动画效果取决于 prefers-reduced-motion 媒体查询。请参阅我们的可访问性文档中的缩减动作部分.

边框旋转器

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

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

颜色

边框旋转器的 border-color 使用 currentColor,这意味着您可以使用文本颜色工具自定义颜色。您可以在标准旋转器上使用我们的任何文字颜色工具。

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 制作,因此您可以使用 text color utilities轻松更改其外观。这里它是蓝色的,还有支持的变体。

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 中的 Spinners 采用 remcurrentColordisplay: inline-flex 构建。这意味着它们可以轻松地调整大小、重新着色并快速对齐。

边距

使用margin utilities,如 .m-5,可轻松拉开间距。

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

位置

使用flexbox 实用工具float 实用工具或[文本对齐方式][文本]实用工具,在任何情况下都能将旋转器精确地放置在您需要的位置。

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 role="status">Loading...</strong>
  <div class="spinner-border ms-auto" aria-hidden="true"></div>
</div>

Floats 浮点布局

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-ssm.spinner-grow-ssm,制作一个更小的旋转器,可快速用于其他组件。

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" aria-hidden="true"></span>
  <span class="visually-hidden" role="status">Loading...</span>
</button>
<button class="btn btn-primary" type="button" disabled>
  <span class="spinner-border spinner-border-sm" aria-hidden="true"></span>
  <span role="status">Loading...</span>
</button>
html
<button class="btn btn-primary" type="button" disabled>
  <span class="spinner-grow spinner-grow-sm" aria-hidden="true"></span>
  <span class="visually-hidden" role="status">Loading...</span>
</button>
<button class="btn btn-primary" type="button" disabled>
  <span class="spinner-grow spinner-grow-sm" aria-hidden="true"></span>
  <span role="status">Loading...</span>
</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-ssm 类的作用如下:

--#{$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;
  }
}