Bootstrap4 进度条
核对 Bootstrap 4 进度条的双层结构、高度自定义、文本标签、语义配色、条纹与动画效果以及多段堆叠进度条的离线 Chromium 实测表现。
学完这一章,你能够
- 核对 Bootstrap 4 进度条的双层结构、高度自定义、文本标签、语义配色、条纹与动画效果以及多段堆叠进度条的离线 Chromium 实测表现。
基础结构、高度控制与进度标签
进度条用于直观反馈上传、加载或任务完成比例。构建基础进度条分为三步:外层 `<div>` 添加 `.progress` 作为浅灰圆角轨道,内层空 `<div>` 添加 `.progress-bar` 作为填充条,再为内层通过行内 `style="width:70%"`(或宽度工具类)设定完成百分比。在 `400px` 宽容器的离线 Chromium 实测中,`70%` 填充条精确渲染为 `280px` 宽。
`.progress` 轨道默认高度为 `1rem`(实测 `16px`),在 `.progress` 上通过 CSS `height` 属性即可改写整体高度(如 `style="height:20px;"` 实测轨道与内部 `40%` 填充条高度同步变为 `20px`、宽 `160px`)。若需直接显示进度数值,只需在 `.progress-bar` 标签体内写入文本(如 `70%`),内置弹性居中样式会使白色文字(`rgb(255, 255, 255)`、`text-align: center`)在条内居中展示。
语义配色、条纹与循环动画
默认填充色为主蓝色 `rgb(0, 123, 255)`,为 `.progress-bar` 组合背景工具类 `.bg-success`、`.bg-info`、`.bg-warning`、`.bg-danger` 即可切换语义状态。实测在 `400px` 轨道中四者分别呈现绿色 `rgb(40, 167, 69)`(`160px`)、青蓝 `rgb(23, 162, 184)`(`200px`)、明黄 `rgb(255, 193, 7)`(`240px`)与警示红 `rgb(220, 53, 69)`(`280px`)。
添加 `.progress-bar-striped` 后,Bootstrap 4 通过 45 度半透明白色 `linear-gradient` 在纯色背景上叠加周期 `16px 16px`(`1rem 1rem`)的斜向条纹;再叠加 `.progress-bar-animated` 即可绑定 `progress-bar-stripes` 关键帧动画(实测 `animationDuration: 1s`、`animationIterationCount: infinite`)。实际使用时,动画条纹 `.progress-bar` 仍须置于外层 `.progress` 轨道中才能获得圆角裁切与默认高度。
混合色彩堆叠进度条
因为 `.progress` 本身采用 `display: flex`,在同一 `.progress` 容器内并列放入多个不同颜色的 `.progress-bar` 子元素即可组成多段混合进度条。实测在 `400px` 容器内依次放置 `40%` 绿色(Free Space)、`10%` 黄色(Warning)与 `20%` 红色(Danger)三段,三者无缝水平衔接,实测宽度分别为 `160px`、`40px` 与 `80px`,合计占据 `280px`(`70%`)。
运行 Bootstrap 4 进度条高度、配色、动画条纹与堆叠布局 Chromium 实验
"""Offline Playwright Chromium verification for Bootstrap 4 Progress Bars."""
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; }
.wrap { width: 400px; }
@keyframes progress-bar-stripes {
from { background-position: 1rem 0; }
to { background-position: 0 0; }
}
.progress {
display: flex; height: 1rem; overflow: hidden; font-size: 0.75rem;
background-color: #e9ecef; border-radius: 0.25rem; margin-bottom: 12px;
}
.progress-bar {
display: flex; flex-direction: column; justify-content: center; overflow: hidden;
color: #fff; text-align: center; white-space: nowrap; background-color: #007bff;
transition: width 0.6s ease;
}
.progress-bar-striped {
background-image: linear-gradient(45deg, rgba(255,255,255,.15) 25%, transparent 25%, transparent 50%, rgba(255,255,255,.15) 50%, rgba(255,255,255,.15) 75%, transparent 75%, transparent);
background-size: 1rem 1rem;
}
.progress-bar-animated { animation: progress-bar-stripes 1s linear infinite; }
.bg-success { background-color: #28a745 !important; }
.bg-info { background-color: #17a2b8 !important; }
.bg-warning { background-color: #ffc107 !important; }
.bg-danger { background-color: #dc3545 !important; }
"""
HTML = f"""<!doctype html>
<html lang="zh-CN"><head><meta charset="utf-8"><style>{CSS}</style></head>
<body>
<div class="wrap">
<div class="progress" id="p-basic"><div class="progress-bar" id="pb-basic" style="width:70%"></div></div>
<div class="progress" id="p-tall" style="height:20px;"><div class="progress-bar" id="pb-tall" style="width:40%;"></div></div>
<div class="progress" id="p-label"><div class="progress-bar" id="pb-label" style="width:70%">70%</div></div>
<div class="progress"><div class="progress-bar bg-success" id="pb-success" style="width:40%"></div></div>
<div class="progress"><div class="progress-bar bg-info" id="pb-info" style="width:50%"></div></div>
<div class="progress"><div class="progress-bar bg-warning" id="pb-warning" style="width:60%"></div></div>
<div class="progress"><div class="progress-bar bg-danger" id="pb-danger" style="width:70%"></div></div>
<div class="progress"><div class="progress-bar progress-bar-striped" id="pb-striped" style="width:40%"></div></div>
<div class="progress"><div class="progress-bar progress-bar-striped progress-bar-animated" id="pb-anim" style="width:40%"></div></div>
<div class="progress" id="p-multi">
<div class="progress-bar bg-success" id="m-free" style="width:40%">Free Space</div>
<div class="progress-bar bg-warning" id="m-warn" style="width:10%">Warning</div>
<div class="progress-bar bg-danger" id="m-danger" style="width:20%">Danger</div>
</div>
</div>
</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": 800})
page.set_content(HTML, wait_until="load")
c1_actual = page.evaluate("""() => {
const pBasic = document.getElementById('p-basic').getBoundingClientRect();
const pbBasic = document.getElementById('pb-basic').getBoundingClientRect();
const pTall = document.getElementById('p-tall').getBoundingClientRect();
const pbTall = document.getElementById('pb-tall').getBoundingClientRect();
const pbLabel = document.getElementById('pb-label');
const stL = getComputedStyle(pbLabel);
return {
default_height_px: Math.round(pBasic.height),
basic_bar_width_px: Math.round(pbBasic.width),
custom_height_px: Math.round(pTall.height),
tall_bar_width_px: Math.round(pbTall.width),
label_text: pbLabel.textContent,
label_color: stL.color,
label_align: stL.textAlign
};
}""")
c1_exp = {
"default_height_px": 16,
"basic_bar_width_px": 280,
"custom_height_px": 20,
"tall_bar_width_px": 160,
"label_text": "70%",
"label_color": "rgb(255, 255, 255)",
"label_align": "center",
}
c2_actual = page.evaluate("""() => {
const ids = ['pb-basic', 'pb-success', 'pb-info', 'pb-warning', 'pb-danger'];
const res = {};
for (const id of ids) {
const el = document.getElementById(id);
res[id] = [getComputedStyle(el).backgroundColor, Math.round(el.getBoundingClientRect().width)];
}
return res;
}""")
c2_exp = {
"pb-basic": ["rgb(0, 123, 255)", 280],
"pb-success": ["rgb(40, 167, 69)", 160],
"pb-info": ["rgb(23, 162, 184)", 200],
"pb-warning": ["rgb(255, 193, 7)", 240],
"pb-danger": ["rgb(220, 53, 69)", 280],
}
c3_actual = page.evaluate("""() => {
const s = getComputedStyle(document.getElementById('pb-striped'));
const a = getComputedStyle(document.getElementById('pb-anim'));
return {
striped_has_gradient: s.backgroundImage.startsWith('linear-gradient('),
striped_bg_size: s.backgroundSize,
striped_anim_name: s.animationName,
animated_name: a.animationName,
animated_duration: a.animationDuration,
animated_iteration: a.animationIterationCount
};
}""")
c3_exp = {
"striped_has_gradient": True,
"striped_bg_size": "16px 16px",
"striped_anim_name": "none",
"animated_name": "progress-bar-stripes",
"animated_duration": "1s",
"animated_iteration": "infinite",
}
c4_actual = page.evaluate("""() => {
const r1 = document.getElementById('m-free').getBoundingClientRect();
const r2 = document.getElementById('m-warn').getBoundingClientRect();
const r3 = document.getElementById('m-danger').getBoundingClientRect();
return {
widths_px: [Math.round(r1.width), Math.round(r2.width), Math.round(r3.width)],
total_width_px: Math.round(r1.width + r2.width + r3.width),
contiguous_row: Math.abs(r1.top - r2.top) < 1 && Math.abs(r2.left - r1.right) < 1 && Math.abs(r3.left - r2.right) < 1
};
}""")
c4_exp = {
"widths_px": [160, 40, 80],
"total_width_px": 280,
"contiguous_row": True,
}
cases = [
{"name": "basic_height_and_label", "input": "400px track default 16px, custom 20px, labeled 70%", "expected": c1_exp, "actual": c1_actual, "passed": c1_actual == c1_exp},
{"name": "contextual_progress_colors", "input": "default + bg-success/info/warning/danger widths", "expected": c2_exp, "actual": c2_actual, "passed": c2_actual == c2_exp},
{"name": "striped_and_animated_bars", "input": ".progress-bar-striped & .progress-bar-animated", "expected": c3_exp, "actual": c3_actual, "passed": c3_actual == c3_exp},
{"name": "stacked_multi_color_bar", "input": "40% + 10% + 20% stacked bars in 400px .progress", "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()
- 核对 16px 默认高度与 20px 自定义高度、70% 白色居中标签、4 种语义色宽度与色值、16px 渐变条纹与 1s 无限动画,以及 40%+10%+20% 无缝堆叠。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:116a1651401cda260b41a5ad2a32721e1a6c22761c15f4f7ad97488fcb7dd8f7
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:d25e3cbd123ca77e913ba1cf0fc98be590d7010933c995840b1a0dd13d6ea704
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
为什么在同一个 `.progress` 内放多个 `.progress-bar` 会横向拼接而非换行?要让条纹动起来必须同时具备哪两个类?
展开参考解答
`.progress` 使用 `display: flex` 布局,因此多个 `.progress-bar` 沿主轴紧邻排列;动画条纹需同时添加 `.progress-bar-striped`(提供渐变条纹背景)和 `.progress-bar-animated`(驱动背景位移动画)。