Bootstrap4 加载
核对 Bootstrap 4 旋转边框(.spinner-border)与脉冲闪烁(.spinner-grow)两类加载指示器的着色机制、小尺寸修饰类及按钮内嵌实测表现。
学完这一章,你能够
- 核对 Bootstrap 4 旋转边框(.spinner-border)与脉冲闪烁(.spinner-grow)两类加载指示器的着色机制、小尺寸修饰类及按钮内嵌实测表现。
旋转边框加载与文本颜色着色
Bootstrap 4 提供纯 CSS 实现的加载指示器,使用 `.spinner-border` 即可创建环形旋转动画。本站离线 Chromium 实测其默认尺寸为 `32px × 32px`(`2rem`),`border-radius: 50%`,右侧边框设为透明 `rgba(0, 0, 0, 0)` 以形成缺口,并绑定 `0.75s` 线性无限循环的 `spinner-border` 旋转动画。
由于 `.spinner-border` 的边框色取自 `currentColor`,直接组合文本颜色工具类(`.text-muted`、`.text-primary`、`.text-success`、`.text-info`、`.text-warning`、`.text-danger`、`.text-secondary`、`.text-dark`、`.text-light`)即可同步改变加载环颜色。实测 `.text-primary`、`.text-success`、`.text-danger` 的计算上边框色分别为 `rgb(0, 123, 255)`、`rgb(40, 167, 69)` 与 `rgb(220, 53, 69)`。
闪烁加载与尺寸缩放
若偏好由中心向外扩张渐隐的脉冲闪烁风格,可改用 `.spinner-grow` 并搭配相同的 `.text-*` 工具类。实测 `.spinner-grow` 同样默认 `32px × 32px` 圆形,通过 `background-color: currentColor` 着色(`.text-primary` 为 `rgb(0, 123, 255)`,`.text-info` 为 `rgb(23, 162, 184)`),绑定 `0.75s` 的 `spinner-grow` 缩放透明度动画。
需要更紧凑的指示器(例如嵌入按钮或表格单元格)时,可附加 `.spinner-border-sm` 或 `.spinner-grow-sm` 类(注意必须与对应的基础类同时书写,仅写基础类加颜色类仍为默认大尺寸)。实测标准 `.spinner-border` 为 `32px`、边框 `4px`,附加 `.spinner-border-sm` 后缩小至 `16px × 16px`、Chromium 像素取整后的计算边框宽为 `3px`;`.spinner-grow-sm` 亦缩小为 `16px × 16px`。
按钮内嵌加载状态
在 `<button class="btn btn-primary">` 内部放入 `<span class="spinner-border spinner-border-sm"></span>` 或 `<span class="spinner-grow spinner-grow-sm"></span>`,可构造纯图标或“图标 + 文案(如 Loading..)”的加载按钮,并常配合 `disabled` 属性防止表单重复提交。实测带 `disabled` 的按钮 `disabled` 状态为 `true`、计算不透明度降至 `0.65`,内部 `<span>` 稳定保持 `16px × 16px` 行内对齐。
运行 Bootstrap 4 旋转/闪烁加载器、小尺寸与加载按钮 Chromium 离线实验
展开查看收起
"""Offline Playwright Chromium verification for Bootstrap 4 Spinners."""
import json
from playwright.sync_api import sync_playwright
CSS = """
*, *::before, *::after { box-sizing: border-box; }
body { margin: 16px; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; font-size: 16px; }
@keyframes spinner-border { to { transform: rotate(360deg); } }
@keyframes spinner-grow {
0% { transform: scale(0); }
50% { opacity: 1; transform: none; }
}
.spinner-border {
display: inline-block; width: 2rem; height: 2rem; vertical-align: text-bottom;
border: 0.25em solid currentColor; border-right-color: transparent;
border-radius: 50%; animation: spinner-border .75s linear infinite;
}
.spinner-border-sm { width: 1rem; height: 1rem; border-width: 0.2em; }
.spinner-grow {
display: inline-block; width: 2rem; height: 2rem; vertical-align: text-bottom;
background-color: currentColor; border-radius: 50%; opacity: 0;
animation: spinner-grow .75s linear infinite;
}
.spinner-grow-sm { width: 1rem; height: 1rem; }
.text-muted { color: #6c757d !important; }
.text-primary { color: #007bff !important; }
.text-success { color: #28a745 !important; }
.text-info { color: #17a2b8 !important; }
.text-warning { color: #ffc107 !important; }
.text-danger { color: #dc3545 !important; }
.text-secondary { color: #6c757d !important; }
.text-dark { color: #343a40 !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.25rem;
}
.btn-primary { color: #fff; background-color: #007bff; border-color: #007bff; }
.btn:disabled { opacity: 0.65; }
"""
HTML = f"""<!doctype html>
<html lang="zh-CN"><head><meta charset="utf-8"><style>{CSS}</style></head>
<body>
<div class="spinner-border" id="sb-default"></div>
<div class="spinner-border text-muted" id="sb-muted"></div>
<div class="spinner-border text-primary" id="sb-primary"></div>
<div class="spinner-border text-success" id="sb-success"></div>
<div class="spinner-border text-info" id="sb-info"></div>
<div class="spinner-border text-warning" id="sb-warning"></div>
<div class="spinner-border text-danger" id="sb-danger"></div>
<div class="spinner-border text-secondary" id="sb-secondary"></div>
<div class="spinner-border text-dark" id="sb-dark"></div>
<div class="spinner-border text-light" id="sb-light"></div>
<div class="spinner-grow text-primary" id="sg-primary"></div>
<div class="spinner-grow text-info" id="sg-info"></div>
<div class="spinner-border spinner-border-sm" id="sb-sm"></div>
<div class="spinner-grow spinner-grow-sm" id="sg-sm"></div>
<button class="btn btn-primary" id="btn-1"><span class="spinner-border spinner-border-sm" id="btn-sp1"></span></button>
<button class="btn btn-primary" id="btn-2"><span class="spinner-border spinner-border-sm"></span> Loading..</button>
<button class="btn btn-primary" id="btn-3" disabled><span class="spinner-border spinner-border-sm"></span> Loading..</button>
<button class="btn btn-primary" id="btn-4" disabled><span class="spinner-grow spinner-grow-sm" id="btn-sp4"></span> Loading..</button>
</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)
try:
page = browser.new_page(viewport={"width": 1000, "height": 600})
page.set_content(HTML, wait_until="load")
c1_actual = page.evaluate("""() => {
const d = getComputedStyle(document.getElementById('sb-default'));
const pr = getComputedStyle(document.getElementById('sb-primary'));
const su = getComputedStyle(document.getElementById('sb-success'));
const da = getComputedStyle(document.getElementById('sb-danger'));
return {
size_px: [d.width, d.height],
border_radius: d.borderRadius,
border_right_color: d.borderRightColor,
animation: [d.animationName, d.animationDuration],
primary_border_top: pr.borderTopColor,
success_border_top: su.borderTopColor,
danger_border_top: da.borderTopColor
};
}""")
c1_exp = {
"size_px": ["32px", "32px"],
"border_radius": "50%",
"border_right_color": "rgba(0, 0, 0, 0)",
"animation": ["spinner-border", "0.75s"],
"primary_border_top": "rgb(0, 123, 255)",
"success_border_top": "rgb(40, 167, 69)",
"danger_border_top": "rgb(220, 53, 69)",
}
c2_actual = page.evaluate("""() => {
const p1 = getComputedStyle(document.getElementById('sg-primary'));
const i1 = getComputedStyle(document.getElementById('sg-info'));
return {
size_px: [p1.width, p1.height],
border_radius: p1.borderRadius,
animation: [p1.animationName, p1.animationDuration],
primary_bg: p1.backgroundColor,
info_bg: i1.backgroundColor
};
}""")
c2_exp = {
"size_px": ["32px", "32px"],
"border_radius": "50%",
"animation": ["spinner-grow", "0.75s"],
"primary_bg": "rgb(0, 123, 255)",
"info_bg": "rgb(23, 162, 184)",
}
c3_actual = page.evaluate("""() => {
const bDef = getComputedStyle(document.getElementById('sb-default'));
const bSm = getComputedStyle(document.getElementById('sb-sm'));
const gSm = getComputedStyle(document.getElementById('sg-sm'));
return {
default_px: [bDef.width, bDef.borderTopWidth],
border_sm_px: [bSm.width, bSm.height, Number(parseFloat(bSm.borderTopWidth).toFixed(1))],
grow_sm_px: [gSm.width, gSm.height]
};
}""")
c3_exp = {
"default_px": ["32px", "4px"],
"border_sm_px": ["16px", "16px", 3],
"grow_sm_px": ["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 sp1 = getComputedStyle(document.getElementById('btn-sp1'));
const sp4 = getComputedStyle(document.getElementById('btn-sp4'));
return {
btn1_disabled: b1.disabled,
btn3_disabled: b3.disabled,
btn3_opacity: getComputedStyle(b3).opacity,
btn4_disabled: b4.disabled,
inline_border_sm_size: [sp1.width, sp1.height],
inline_grow_sm_size: [sp4.width, sp4.height]
};
}""")
c4_exp = {
"btn1_disabled": False,
"btn3_disabled": True,
"btn3_opacity": "0.65",
"btn4_disabled": True,
"inline_border_sm_size": ["16px", "16px"],
"inline_grow_sm_size": ["16px", "16px"],
}
cases = [
{"name": "spinner_border_geometry_and_colors", "input": ".spinner-border default + text-* contextual colors", "expected": c1_exp, "actual": c1_actual, "passed": c1_actual == c1_exp},
{"name": "spinner_grow_animation_and_colors", "input": ".spinner-grow with text-primary/text-info", "expected": c2_exp, "actual": c2_actual, "passed": c2_actual == c2_exp},
{"name": "spinner_small_sizing", "input": ".spinner-border-sm & .spinner-grow-sm vs default 2rem", "expected": c3_exp, "actual": c3_actual, "passed": c3_actual == c3_exp},
{"name": "spinner_inside_buttons", "input": "active & disabled loading buttons with inline spinners", "expected": c4_exp, "actual": c4_actual, "passed": c4_actual == c4_exp},
]
print(json.dumps({"cases": cases}, ensure_ascii=False))
finally:
browser.close()
if __name__ == "__main__":
main()
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
`.spinner-border` 与 `.spinner-grow` 分别通过 `border-color: currentColor` 和 `background-color: currentColor` 继承文字颜色,因此统一用 `.text-*` 着色;嵌入按钮时建议使用行内 `<span>` 搭配 `.spinner-border-sm` 或 `.spinner-grow-sm`(`16px`),并按需加 `disabled` 防重复点击。