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

离线验证 Bootstrap 4 按钮色彩、多标签一致性、轮廓悬停、尺寸/块级、禁点与加载动画

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-buttons.py
explore-buttons.py277 行
"""Offline Playwright Chromium lab for Bootstrap 4 Ch 10 (buttons)."""
import json
from playwright.sync_api import sync_playwright

BS4_BTN_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 0; padding: 16px; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 1rem; line-height: 1.5; background: #fff; }
.btn {
  display: inline-block;
  font-family: inherit;
  font-weight: 400;
  color: #212529;
  text-align: center;
  vertical-align: middle;
  user-select: none;
  background-color: transparent;
  border: 1px solid transparent;
  padding: 0.375rem 0.75rem;
  font-size: 1rem;
  line-height: 1.5;
  border-radius: 0.25rem;
  text-decoration: none;
  cursor: pointer;
}
.btn-primary { color: #fff; background-color: #007bff; border-color: #007bff; }
.btn-primary.active { color: #fff; background-color: #0062cc; border-color: #005cbf; }
.btn-secondary { color: #fff; background-color: #6c757d; border-color: #6c757d; }
.btn-success { color: #fff; background-color: #28a745; border-color: #28a745; }
.btn-info { color: #fff; background-color: #17a2b8; border-color: #17a2b8; }
.btn-warning { color: #212529; background-color: #ffc107; border-color: #ffc107; }
.btn-danger { color: #fff; background-color: #dc3545; border-color: #dc3545; }
.btn-dark { color: #fff; background-color: #343a40; border-color: #343a40; }
.btn-light { color: #212529; background-color: #f8f9fa; border-color: #f8f9fa; }
.btn-link { font-weight: 400; color: #007bff; text-decoration: none; background-color: transparent; border-color: transparent; }

.btn-outline-primary { color: #007bff; border-color: #007bff; background-color: transparent; }
.btn-outline-primary:hover { color: #fff; background-color: #007bff; border-color: #007bff; }
.btn-outline-secondary { color: #6c757d; border-color: #6c757d; }
.btn-outline-success { color: #28a745; border-color: #28a745; }
.btn-outline-info { color: #17a2b8; border-color: #17a2b8; }
.btn-outline-warning { color: #ffc107; border-color: #ffc107; }
.btn-outline-danger { color: #dc3545; border-color: #dc3545; }
.btn-outline-dark { color: #343a40; border-color: #343a40; }
.btn-outline-light { color: #f8f9fa; border-color: #f8f9fa; }
.text-dark { color: #343a40 !important; }

.btn-lg { padding: 0.5rem 1rem; font-size: 1.25rem; line-height: 1.5; border-radius: 0.3rem; }
.btn-sm { padding: 0.25rem 0.5rem; font-size: 0.875rem; line-height: 1.5; border-radius: 0.2rem; }
.btn-block { display: block; width: 100%; }

.btn.disabled, .btn:disabled { opacity: 0.65; }
a.btn.disabled { pointer-events: none; }

@keyframes spinner-border { to { transform: rotate(360deg); } }
@keyframes spinner-grow { 0% { transform: scale(0); } 50% { opacity: 1; } }
.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 0.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 0.75s linear infinite; }
.spinner-grow-sm { width: 1rem; height: 1rem; }
.block-wrap { width: 600px; }
"""

HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{BS4_BTN_CSS}</style></head>
<body>
  <div id="solid-group">
    <button type="button" class="btn" data-k="base">基本</button>
    <button type="button" class="btn btn-primary" data-k="primary">主要</button>
    <button type="button" class="btn btn-secondary" data-k="secondary">次要</button>
    <button type="button" class="btn btn-success" data-k="success">成功</button>
    <button type="button" class="btn btn-info" data-k="info">信息</button>
    <button type="button" class="btn btn-warning" data-k="warning">警告</button>
    <button type="button" class="btn btn-danger" data-k="danger">危险</button>
    <button type="button" class="btn btn-dark" data-k="dark">黑色</button>
    <button type="button" class="btn btn-light" data-k="light">浅色</button>
    <button type="button" class="btn btn-link" data-k="link">链接</button>
  </div>

  <div id="tag-group">
    <a id="tag-a" href="#" class="btn btn-info" role="button">链接按钮</a>
    <button id="tag-btn" type="button" class="btn btn-info">按钮</button>
    <input id="tag-ib" type="button" class="btn btn-info" value="输入框按钮">
    <input id="tag-is" type="submit" class="btn btn-info" value="提交按钮">
  </div>

  <div id="outline-group">
    <button id="out-primary" type="button" class="btn btn-outline-primary">主要边框</button>
    <button id="out-light" type="button" class="btn btn-outline-light text-dark">浅色边框</button>
  </div>

  <div id="size-group">
    <button id="sz-lg" type="button" class="btn btn-primary btn-lg">大号</button>
    <button id="sz-md" type="button" class="btn btn-primary">默认</button>
    <button id="sz-sm" type="button" class="btn btn-primary btn-sm">小号</button>
  </div>

  <div class="block-wrap">
    <button id="btn-blk" type="button" class="btn btn-primary btn-block">块级按钮</button>
  </div>

  <div id="state-group">
    <button id="st-active" type="button" class="btn btn-primary active">激活按钮</button>
    <button id="st-disabled" type="button" class="btn btn-primary" disabled>禁用按钮</button>
    <a id="st-link-disabled" href="#blocked" class="btn btn-primary disabled">禁用链接</a>
  </div>

  <div id="spinner-group">
    <button id="sp-b1" type="button" class="btn btn-primary"><span id="sp-border" class="spinner-border spinner-border-sm"></span> Loading..</button>
    <button id="sp-b2" type="button" class="btn btn-primary" disabled><span id="sp-grow" class="spinner-grow spinner-grow-sm"></span> Loading..</button>
  </div>

  <script>
    window.__clicks = {{ active: 0, disabledBtn: 0, disabledLink: 0 }};
    document.getElementById('st-active').addEventListener('click', () => window.__clicks.active++);
    document.getElementById('st-disabled').addEventListener('click', () => window.__clicks.disabledBtn++);
    document.getElementById('st-link-disabled').addEventListener('click', (e) => {{ e.preventDefault(); window.__clicks.disabledLink++; }});
  </script>
</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():
    cases = []
    with sync_playwright() as p:
        browser = launch_browser(p)
        page = browser.new_page(viewport={"width": 900, "height": 900})
        page.set_content(HTML_PAGE)

        # Case 1: solid_and_link_button_styles
        solid = page.evaluate("""() => {
            const out = {};
            for (const el of document.querySelectorAll('#solid-group button')) {
                const s = getComputedStyle(el);
                out[el.dataset.k] = { bg: s.backgroundColor, color: s.color };
            }
            return out;
        }""")
        expected_1 = {
            "base": {"bg": "rgba(0, 0, 0, 0)", "color": "rgb(33, 37, 41)"},
            "primary": {"bg": "rgb(0, 123, 255)", "color": "rgb(255, 255, 255)"},
            "secondary": {"bg": "rgb(108, 117, 125)", "color": "rgb(255, 255, 255)"},
            "success": {"bg": "rgb(40, 167, 69)", "color": "rgb(255, 255, 255)"},
            "info": {"bg": "rgb(23, 162, 184)", "color": "rgb(255, 255, 255)"},
            "warning": {"bg": "rgb(255, 193, 7)", "color": "rgb(33, 37, 41)"},
            "danger": {"bg": "rgb(220, 53, 69)", "color": "rgb(255, 255, 255)"},
            "dark": {"bg": "rgb(52, 58, 64)", "color": "rgb(255, 255, 255)"},
            "light": {"bg": "rgb(248, 249, 250)", "color": "rgb(33, 37, 41)"},
            "link": {"bg": "rgba(0, 0, 0, 0)", "color": "rgb(0, 123, 255)"},
        }
        cases.append(dict(name="solid_and_link_button_styles", passed=(solid == expected_1), expected=expected_1, actual=solid))

        # Case 2: button_tags_and_outline_styles
        tags = page.evaluate("""() => {
            return ['tag-a', 'tag-btn', 'tag-ib', 'tag-is'].map(id => {
                const el = document.getElementById(id);
                const s = getComputedStyle(el);
                return { tag: el.tagName.toLowerCase(), bg: s.backgroundColor, h: Math.round(el.getBoundingClientRect().height) };
            });
        }""")
        out_before = page.evaluate("""() => {
            const s = getComputedStyle(document.getElementById('out-primary'));
            return { bg: s.backgroundColor, border: s.borderTopColor, color: s.color };
        }""")
        page.locator("#out-primary").hover()
        out_hover = page.evaluate("""() => {
            const s = getComputedStyle(document.getElementById('out-primary'));
            return { bg: s.backgroundColor, color: s.color };
        }""")
        actual_2 = {"four_tags": tags, "outline_normal": out_before, "outline_hover": out_hover}
        expected_2 = {
            "four_tags": [
                {"tag": "a", "bg": "rgb(23, 162, 184)", "h": 38},
                {"tag": "button", "bg": "rgb(23, 162, 184)", "h": 38},
                {"tag": "input", "bg": "rgb(23, 162, 184)", "h": 38},
                {"tag": "input", "bg": "rgb(23, 162, 184)", "h": 38},
            ],
            "outline_normal": {"bg": "rgba(0, 0, 0, 0)", "border": "rgb(0, 123, 255)", "color": "rgb(0, 123, 255)"},
            "outline_hover": {"bg": "rgb(0, 123, 255)", "color": "rgb(255, 255, 255)"},
        }
        cases.append(dict(name="button_tags_and_outline_styles", passed=(actual_2 == expected_2), expected=expected_2, actual=actual_2))

        # Case 3: button_sizes_and_block
        sizes = page.evaluate("""() => {
            const m = id => {
                const el = document.getElementById(id);
                const s = getComputedStyle(el);
                return { font: parseFloat(s.fontSize), pad_y: parseFloat(s.paddingTop), h: Math.round(el.getBoundingClientRect().height) };
            };
            const blk = document.getElementById('btn-blk');
            return {
                lg: m('sz-lg'),
                default: m('sz-md'),
                sm: m('sz-sm'),
                block_display: getComputedStyle(blk).display,
                block_width: Math.round(blk.getBoundingClientRect().width),
            };
        }""")
        expected_3 = {
            "lg": {"font": 20.0, "pad_y": 8.0, "h": 48},
            "default": {"font": 16.0, "pad_y": 6.0, "h": 38},
            "sm": {"font": 14.0, "pad_y": 4.0, "h": 31},
            "block_display": "block",
            "block_width": 600,
        }
        cases.append(dict(name="button_sizes_and_block", passed=(sizes == expected_3), expected=expected_3, actual=sizes))

        # Case 4: active_and_disabled_interaction
        page.click("#st-active")
        # Attempt clicking disabled button via dispatch / force check and inspect pointer-events on disabled anchor
        page.evaluate("() => { document.getElementById('st-disabled').click(); }")
        states = page.evaluate("""() => {
            const act = getComputedStyle(document.getElementById('st-active'));
            const disBtn = getComputedStyle(document.getElementById('st-disabled'));
            const disLink = getComputedStyle(document.getElementById('st-link-disabled'));
            return {
                active_bg: act.backgroundColor,
                disabled_btn_opacity: disBtn.opacity,
                disabled_link_opacity: disLink.opacity,
                disabled_link_pointer_events: disLink.pointerEvents,
                clicks: window.__clicks,
            };
        }""")
        expected_4 = {
            "active_bg": "rgb(0, 98, 204)",
            "disabled_btn_opacity": "0.65",
            "disabled_link_opacity": "0.65",
            "disabled_link_pointer_events": "none",
            "clicks": {"active": 1, "disabledBtn": 0, "disabledLink": 0},
        }
        cases.append(dict(name="active_and_disabled_interaction", passed=(states == expected_4), expected=expected_4, actual=states))

        # Case 5: spinner_loading_buttons
        spin = page.evaluate("""() => {
            const sb = getComputedStyle(document.getElementById('sp-border'));
            const sg = getComputedStyle(document.getElementById('sp-grow'));
            const b2 = document.getElementById('sp-b2');
            return {
                border_size: [parseFloat(sb.width), parseFloat(sb.height)],
                border_radius: sb.borderRadius,
                border_right_transparent: sb.borderRightColor === 'rgba(0, 0, 0, 0)',
                border_anim: sb.animationName,
                grow_size: [parseFloat(sg.width), parseFloat(sg.height)],
                grow_radius: sg.borderRadius,
                grow_anim: sg.animationName,
                loading_btn_disabled: b2.disabled,
            };
        }""")
        expected_5 = {
            "border_size": [16.0, 16.0],
            "border_radius": "50%",
            "border_right_transparent": True,
            "border_anim": "spinner-border",
            "grow_size": [16.0, 16.0],
            "grow_radius": "50%",
            "grow_anim": "spinner-grow",
            "loading_btn_disabled": True,
        }
        cases.append(dict(name="spinner_loading_buttons", passed=(spin == expected_5), expected=expected_5, actual=spin))

        browser.close()

    print(json.dumps({"cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

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

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

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

下载课程关联工程包:Bootstrap4

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

下载课程关联工程包:Bootstrap4

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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