Bootstrap5 加载效果
核对 Bootstrap 5 边框旋转与脉冲闪烁两类加载器、text-* 颜色继承、小尺寸变体及加载按钮组合。
学完这一章,你能够
- 核对 Bootstrap 5 边框旋转与脉冲闪烁两类加载器、text-* 颜色继承、小尺寸变体及加载按钮组合。
边框旋转加载器与文本色控制
Bootstrap 5 提供纯 CSS 实现的加载指示器(Spinners),无需图片或 JavaScript。最常用的是 `.spinner-border`,它通过将圆形边框右侧设为透明并叠加 360 度旋转关键帧形成缺口圆环动画。本站离线 Chromium 实测默认 `.spinner-border` 尺寸为 `32px × 32px`、`border-radius: 50%`、`border-right-color: rgba(0, 0, 0, 0)`,动画名为 `spinner-border`、周期 `0.75s`。
由于边框颜色绑定 `currentColor`,直接叠加文本颜色工具类(`.text-muted`、`.text-primary`、`.text-success`、`.text-info`、`.text-warning`、`.text-danger`、`.text-secondary`、`.text-dark`、`.text-light`)即可定制加载器色彩。实测 9 种文本色均准确传导至 `border-top-color`(如 `.text-primary` 为 `rgb(13, 110, 253)`)。
脉冲闪烁加载效果
若偏好由中心向外扩张渐隐的脉冲动画,可使用 `.spinner-grow` 并配合相同的 `.text-*` 工具类。实测 `.spinner-grow` 尺寸同样为 `32px × 32px`、`border-radius: 50%`,背景色继承 `currentColor` 并绑定 `spinner-grow` 关键帧动画。
加载器尺寸与加载按钮
添加 `.spinner-border-sm` 或 `.spinner-grow-sm` 可将加载器由 `2rem` 缩减为紧凑的 `1rem`(`16px × 16px`),便于嵌入按钮或表格行内。实测 Chromium 将 `.spinner-border-sm` 的 `0.2em` 边框宽度像素对齐为 `3px`。
将 `<span>` 形式的小号加载器放入 `.btn` 内,可构建仅图标、图标加提示文以及带 `disabled` 属性的提交等待按钮。实测内嵌 `.spinner-border-sm` 与 `.spinner-grow-sm` 布局宽高均为 `16px × 16px`,禁用态按钮透明降至 `0.65`。
在离线 Chromium 中验证 spinner-border/spinner-grow 动画、text-* 配色、小尺寸及按钮内嵌
"""Offline Playwright Chromium verification for Bootstrap 5 Spinners (Ch 15)."""
import json
from playwright.sync_api import sync_playwright
HTML = """<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<style>
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 16px; font-family: system-ui, -apple-system, "Segoe UI", sans-serif; }
@keyframes spinner-border { to { transform: rotate(360deg); } }
@keyframes spinner-grow {
0% { transform: scale(0); opacity: 0; }
50% { opacity: 1; transform: none; }
}
.spinner-border {
display: inline-block;
width: 2rem;
height: 2rem;
vertical-align: -0.125em;
border: 0.25em solid currentColor;
border-right-color: transparent;
border-radius: 50%;
animation: 0.75s linear infinite spinner-border;
}
.spinner-border-sm { width: 1rem; height: 1rem; border-width: 0.2em; }
.spinner-grow {
display: inline-block;
width: 2rem;
height: 2rem;
vertical-align: -0.125em;
background-color: currentColor;
border-radius: 50%;
opacity: 0;
animation: 0.75s linear infinite spinner-grow;
}
.spinner-grow-sm { width: 1rem; height: 1rem; }
.text-muted { color: #6c757d !important; }
.text-primary { color: #0d6efd !important; }
.text-success { color: #198754 !important; }
.text-info { color: #0dcaf0 !important; }
.text-warning { color: #ffc107 !important; }
.text-danger { color: #dc3545 !important; }
.text-secondary { color: #6c757d !important; }
.text-dark { color: #212529 !important; }
.text-light { color: #f8f9fa !important; }
.btn {
display: inline-block; padding: 0.375rem 0.75rem; font-size: 1rem; line-height: 1.5;
border: 1px solid transparent; border-radius: 0.375rem;
}
.btn-primary { color: #fff; background-color: #0d6efd; border-color: #0d6efd; }
.btn:disabled { pointer-events: none; opacity: 0.65; }
</style>
</head>
<body>
<section id="sec-border">
<div id="sb-default" class="spinner-border" role="status"></div>
<div id="sb-muted" class="spinner-border text-muted"></div>
<div id="sb-primary" class="spinner-border text-primary"></div>
<div id="sb-success" class="spinner-border text-success"></div>
<div id="sb-info" class="spinner-border text-info"></div>
<div id="sb-warning" class="spinner-border text-warning"></div>
<div id="sb-danger" class="spinner-border text-danger"></div>
<div id="sb-secondary" class="spinner-border text-secondary"></div>
<div id="sb-dark" class="spinner-border text-dark"></div>
<div id="sb-light" class="spinner-border text-light"></div>
</section>
<section id="sec-grow">
<div id="sg-primary" class="spinner-grow text-primary"></div>
<div id="sg-success" class="spinner-grow text-success"></div>
<div id="sg-danger" class="spinner-grow text-danger"></div>
</section>
<section id="sec-sm">
<div id="sb-sm" class="spinner-border spinner-border-sm"></div>
<div id="sg-sm" class="spinner-grow spinner-grow-sm"></div>
</section>
<section id="sec-btn">
<button id="btn-1" class="btn btn-primary"><span id="bi-1" class="spinner-border spinner-border-sm"></span></button>
<button id="btn-2" class="btn btn-primary"><span class="spinner-border spinner-border-sm"></span> Loading..</button>
<button id="btn-3" class="btn btn-primary" disabled><span class="spinner-border spinner-border-sm"></span> Loading..</button>
<button id="btn-4" class="btn btn-primary" disabled><span id="bi-4" class="spinner-grow spinner-grow-sm"></span> Loading..</button>
</section>
</body>
</html>
"""
def launch_browser(p):
try:
return p.chromium.launch(headless=True)
except Exception:
return p.chromium.launch(headless=True, channel="chrome")
def main():
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page(viewport={"width": 800, "height": 600})
page.set_content(HTML, wait_until="load")
c1_actual = page.evaluate("""() => {
const s = getComputedStyle(document.getElementById("sb-default"));
const bcol = id => getComputedStyle(document.getElementById(id)).borderTopColor;
return {
width: s.width,
height: s.height,
border_radius: s.borderTopLeftRadius,
border_right_color: s.borderRightColor,
animation_name: s.animationName,
animation_duration: s.animationDuration,
primary_border: bcol("sb-primary"),
success_border: bcol("sb-success"),
danger_border: bcol("sb-danger"),
warning_border: bcol("sb-warning"),
total_colored_borders: document.querySelectorAll("#sec-border .spinner-border").length,
};
}""")
c1_expected = {
"width": "32px",
"height": "32px",
"border_radius": "50%",
"border_right_color": "rgba(0, 0, 0, 0)",
"animation_name": "spinner-border",
"animation_duration": "0.75s",
"primary_border": "rgb(13, 110, 253)",
"success_border": "rgb(25, 135, 84)",
"danger_border": "rgb(220, 53, 69)",
"warning_border": "rgb(255, 193, 7)",
"total_colored_borders": 10,
}
c2_actual = page.evaluate("""() => {
const sp = getComputedStyle(document.getElementById("sg-primary"));
return {
width: sp.width,
height: sp.height,
border_radius: sp.borderTopLeftRadius,
animation_name: sp.animationName,
primary_bg: sp.backgroundColor,
success_bg: getComputedStyle(document.getElementById("sg-success")).backgroundColor,
danger_bg: getComputedStyle(document.getElementById("sg-danger")).backgroundColor,
};
}""")
c2_expected = {
"width": "32px",
"height": "32px",
"border_radius": "50%",
"animation_name": "spinner-grow",
"primary_bg": "rgb(13, 110, 253)",
"success_bg": "rgb(25, 135, 84)",
"danger_bg": "rgb(220, 53, 69)",
}
c3_actual = page.evaluate("""() => {
const b = getComputedStyle(document.getElementById("sb-sm"));
const g = getComputedStyle(document.getElementById("sg-sm"));
return {
border_sm_wh: [b.width, b.height],
border_sm_bw: b.borderTopWidth,
grow_sm_wh: [g.width, g.height],
};
}""")
c3_expected = {
"border_sm_wh": ["16px", "16px"],
"border_sm_bw": "3px",
"grow_sm_wh": ["16px", "16px"],
}
c4_actual = page.evaluate("""() => {
const b1 = document.getElementById("btn-1");
const b3 = document.getElementById("btn-3");
const b4 = document.getElementById("btn-4");
const i1 = getComputedStyle(document.getElementById("bi-1"));
const i4 = getComputedStyle(document.getElementById("bi-4"));
return {
btn1_disabled: b1.disabled,
btn3_disabled: b3.disabled,
btn4_disabled: b4.disabled,
btn3_opacity: getComputedStyle(b3).opacity,
inline_border_sm: [i1.width, i1.height],
inline_grow_sm: [i4.width, i4.height],
};
}""")
c4_expected = {
"btn1_disabled": False,
"btn3_disabled": True,
"btn4_disabled": True,
"btn3_opacity": "0.65",
"inline_border_sm": ["16px", "16px"],
"inline_grow_sm": ["16px", "16px"],
}
browser.close()
cases = [
dict(name="spinner_border_and_colors", description="核对 .spinner-border 32px 圆形透明右边框旋转动画与 text-* currentColor 继承", expected=c1_expected, actual=c1_actual, passed=c1_actual == c1_expected),
dict(name="spinner_grow_and_colors", description="核对 .spinner-grow 脉冲缩放动画与 currentColor 实心背景色", expected=c2_expected, actual=c2_actual, passed=c2_actual == c2_expected),
dict(name="spinner_sizes", description="核对 .spinner-border-sm 与 .spinner-grow-sm 缩至 16px 及 Chromium 像素对齐后 3px 边框宽度", expected=c3_expected, actual=c3_actual, passed=c3_actual == c3_expected),
dict(name="spinner_inside_buttons", description="核对加载按钮内嵌旋转器与禁用态 opacity=0.65 组合", expected=c4_expected, actual=c4_actual, passed=c4_actual == c4_expected),
]
print(json.dumps({"cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
- 核对 32px 默认圆形旋转/脉冲动画、currentColor 传导、16px 小尺寸(边框像素对齐 3px)及禁用加载按钮 opacity=0.65。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:11094e37c4b794cd9b0297a8888ade3bca1031dc2708588917500f61d88a6940
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:7490242d2755d1c85e17f603b25c64e4d0dc7a7ddf7b79ee9ffb8eb92b3da262
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
为什么修改 Bootstrap 5 加载器颜色使用 `.text-*` 工具类而不是 `.bg-*`?
展开参考解答
因为 `.spinner-border` 的边框和 `.spinner-grow` 的实心填充在源码中都使用 `currentColor` 读取当前文字颜色,所以通过 `.text-*` 即可统一控制两类加载器的颜色。