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

离线验证 Bootstrap 4 基础圆角巨幕与全宽无圆角 .jumbotron-fluid

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

BS4_JUMBO_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 0; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 1rem; line-height: 1.5; color: #212529; background: #fff; }
h1 { margin-top: 0; margin-bottom: 0.5rem; font-size: 2.5rem; font-weight: 500; line-height: 1.2; }
p { margin-top: 0; margin-bottom: 1rem; }
hr { margin: 1.5rem 0; border: 0; border-top: 1px solid rgba(0, 0, 0, 0.1); }
.container { width: 100%; padding-right: 15px; padding-left: 15px; margin-right: auto; margin-left: auto; }
@media (min-width: 576px) { .container { max-width: 540px; } }
@media (min-width: 768px) { .container { max-width: 720px; } }
@media (min-width: 992px) { .container { max-width: 960px; } }
@media (min-width: 1200px) { .container { max-width: 1140px; } }
.jumbotron { padding: 2rem 1rem; margin-bottom: 2rem; background-color: #e9ecef; border-radius: 0.3rem; }
@media (min-width: 576px) { .jumbotron { padding: 4rem 2rem; } }
.jumbotron-fluid { padding-right: 0; padding-left: 0; border-radius: 0; }
.btn { display: inline-block; padding: 0.375rem 0.75rem; font-size: 1rem; line-height: 1.5; border-radius: 0.25rem; text-decoration: none; border: 1px solid transparent; }
.btn-primary { color: #fff; background-color: #007bff; border-color: #007bff; }
"""

HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{BS4_JUMBO_CSS}</style></head>
<body>
  <div class="container">
    <div id="jb-basic" class="jumbotron">
      <h1 id="jb-h1">Bootstrap4 巨幕展示区</h1>
      <p id="jb-p">用于突出呈现页面核心导语与关键操作入口。</p>
      <hr id="jb-hr">
      <a id="jb-btn" class="btn btn-primary" href="#">了解更多</a>
    </div>
  </div>
  <div id="jb-fluid" class="jumbotron jumbotron-fluid">
    <div id="jb-fluid-inner" class="container">
      <h1>全宽无圆角巨幕</h1>
      <p>背景铺满整个视口宽度,内部正文仍居中对齐。</p>
    </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():
    cases = []
    with sync_playwright() as p:
        browser = launch_browser(p)
        page = browser.new_page(viewport={"width": 992, "height": 900})
        page.set_content(HTML_PAGE)

        # Case 1: basic_jumbotron_styles
        res1 = page.evaluate("""() => {
            const el = document.getElementById('jb-basic');
            const s = getComputedStyle(el);
            const r = el.getBoundingClientRect();
            return {
                bg: s.backgroundColor,
                radius: s.borderRadius,
                margin_bottom: s.marginBottom,
                padding: s.paddingTop + ' ' + s.paddingRight,
                width_at_992: Math.round(r.width),
            };
        }""")
        expected_1 = {
            "bg": "rgb(233, 236, 239)",
            "radius": "4.8px",
            "margin_bottom": "32px",
            "padding": "64px 32px",
            "width_at_992": 930,
        }
        cases.append(dict(name="basic_jumbotron_styles", passed=(res1 == expected_1), expected=expected_1, actual=res1))

        # Case 2: jumbotron_responsive_padding
        page.set_viewport_size({"width": 375, "height": 900})
        p375 = page.evaluate("() => { const s = getComputedStyle(document.getElementById('jb-basic')); return s.paddingTop + ' ' + s.paddingRight; }")
        page.set_viewport_size({"width": 768, "height": 900})
        p768 = page.evaluate("() => { const s = getComputedStyle(document.getElementById('jb-basic')); return s.paddingTop + ' ' + s.paddingRight; }")
        actual_2 = {"padding_375": p375, "padding_768": p768}
        expected_2 = {"padding_375": "32px 16px", "padding_768": "64px 32px"}
        cases.append(dict(name="jumbotron_responsive_padding", passed=(actual_2 == expected_2), expected=expected_2, actual=actual_2))

        # Case 3: jumbotron_fluid_full_bleed
        page.set_viewport_size({"width": 1200, "height": 900})
        res3 = page.evaluate("""() => {
            const fl = document.getElementById('jb-fluid');
            const inner = document.getElementById('jb-fluid-inner');
            const s = getComputedStyle(fl);
            const r = fl.getBoundingClientRect();
            const ir = inner.getBoundingClientRect();
            return {
                fluid_width: Math.round(r.width),
                fluid_radius: s.borderRadius,
                fluid_padding_x: [s.paddingLeft, s.paddingRight],
                inner_container_width: Math.round(ir.width),
                inner_left_offset: Math.round(ir.left),
            };
        }""")
        expected_3 = {
            "fluid_width": 1200,
            "fluid_radius": "0px",
            "fluid_padding_x": ["0px", "0px"],
            "inner_container_width": 1140,
            "inner_left_offset": 30,
        }
        cases.append(dict(name="jumbotron_fluid_full_bleed", passed=(res3 == expected_3), expected=expected_3, actual=res3))

        # Case 4: jumbotron_nested_elements
        res4 = page.evaluate("""() => {
            const tops = ['jb-h1', 'jb-p', 'jb-hr', 'jb-btn'].map(id => Math.round(document.getElementById(id).getBoundingClientRect().top));
            const btnBg = getComputedStyle(document.getElementById('jb-btn')).backgroundColor;
            return {
                strictly_stacked: tops[0] < tops[1] && tops[1] < tops[2] && tops[2] < tops[3],
                nested_btn_bg: btnBg,
            };
        }""")
        expected_4 = {"strictly_stacked": True, "nested_btn_bg": "rgb(0, 123, 255)"}
        cases.append(dict(name="jumbotron_nested_elements", passed=(res4 == expected_4), expected=expected_4, actual=res4))

        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-jumbotron-lab/explore-jumbotron.py

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

下载课程关联工程包:Bootstrap4

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

下载课程关联工程包:Bootstrap4

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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