蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 Bootstrap 4 旋转/闪烁加载器、小尺寸与加载按钮 Chromium 离线实验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-spinners.py
explore-spinners.py180 行
"""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()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

入口文件:projects/bootstrap4-spinners-lab/explore-spinners.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-spinners.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Bootstrap4

课程关联工程包(登记关联35章) · 含当前章节或实例 · mbskycom202612a664.zip

下载课程关联工程包:Bootstrap4

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a693.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习