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

离线验证 Foundation 5 入门页面、响应式栅格断点与交互组件状态

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

FOUNDATION_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 0; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; font-size: 1rem; line-height: 1.5; color: #222222; background: #ffffff; }
.row { width: 100%; margin: 0 auto; max-width: 62.5rem; }
.row::before, .row::after { content: " "; display: table; }
.row::after { clear: both; }
.columns { position: relative; padding-left: 0.9375rem; padding-right: 0.9375rem; width: 100%; float: left; }
@media only screen and (min-width: 40.0625em) {
  .medium-4 { width: 33.33333%; }
  .medium-6 { width: 50%; }
  .medium-12 { width: 100%; }
}
.panel { border: 1px solid #d8d8d8; margin-bottom: 1.25rem; padding: 1.25rem; background: #f2f2f2; color: #333333; }
.button { display: inline-block; background-color: #008CBA; color: #ffffff; padding: 1rem 2rem 1.0625rem 2rem; font-size: 1rem; border: 0; cursor: pointer; text-decoration: none; }
.button.small { padding: 0.875rem 1.75rem 0.9375rem 1.75rem; font-size: 0.8125rem; }
.alert-box { display: block; width: 100%; padding: 0.875rem 1.5rem 0.875rem 0.875rem; margin-bottom: 1rem; background: #43AC6A; color: #ffffff; position: relative; font-size: 0.8125rem; }
.alert-box .close { position: absolute; right: 0.5rem; top: 0.5rem; color: #ffffff; text-decoration: none; cursor: pointer; }
.progress { background: #f6f6f6; height: 1.5625rem; border: 1px solid #ffffff; padding: 0.125rem; width: 100%; }
.progress .meter { background: #008CBA; height: 100%; display: block; }
table { width: 100%; background: #ffffff; border: 1px solid #dddddd; border-collapse: separate; border-spacing: 0; }
table thead { background: #f5f5f5; }
table tr:nth-of-type(even) { background: #f9f9f9; }
table th, table td { padding: 0.5rem 0.625rem; text-align: left; font-size: 0.875rem; }
.f-dropdown { display: none; list-style: none; margin: 0; padding: 0; border: 1px solid #cccccc; background: #ffffff; width: 180px; }
.f-dropdown.open { display: block; }
.accordion-navigation .content { display: none; padding: 0.75rem; }
.accordion-navigation.active .content { display: block; }
.reveal-modal { display: none; padding: 1.5rem; background: #ffffff; border: 1px solid #666666; }
.reveal-modal.open { display: block; }
"""

HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>{FOUNDATION_CSS}</style>
</head>
<body>
  <div id="hero-row" class="row">
    <div id="hero-col" class="medium-12 columns">
      <div id="hero-panel" class="panel">
        <h1>Foundation 页面</h1>
        <p>调整浏览器窗口大小以验证移动优先响应式栅格。</p>
        <button id="hero-btn" type="button" class="button small">交互按钮</button>
      </div>
    </div>
  </div>
  <div id="grid-row" class="row">
    <div id="col1" class="medium-4 columns"><h3>栅格第一列</h3><p>移动端默认堆叠,中等屏起三列并排。</p></div>
    <div id="col2" class="medium-4 columns"><h3>栅格第二列</h3><p>左右各 15px 内边距形成 30px 列间隙。</p></div>
    <div id="col3" class="medium-4 columns"><h3>栅格第三列</h3><p>行容器最大宽度为 62.5rem(1000px)。</p></div>
  </div>
  <section id="feature-preview" class="row">
    <div class="medium-12 columns">
      <div id="demo-alert" data-alert class="alert-box">操作成功提示<a id="alert-close" href="#" class="close">&times;</a></div>
      <div id="demo-progress" class="progress"><span id="demo-meter" class="meter" style="width:65%"></span></div>
      <table id="demo-table">
        <thead><tr><th>组件</th><th>核心类</th></tr></thead>
        <tbody>
          <tr id="tr-odd"><td>面板</td><td>.panel</td></tr>
          <tr id="tr-even"><td>按钮</td><td>.button</td></tr>
        </tbody>
      </table>
      <button id="dd-trigger" type="button" class="button small">展开菜单</button>
      <ul id="dd-menu" class="f-dropdown"><li>选项 A</li><li>选项 B</li></ul>
      <div id="acc-item" class="accordion-navigation">
        <a id="acc-toggle" href="#">手风琴面板</a>
        <div id="acc-content" class="content">展开后的折叠说明内容</div>
      </div>
      <button id="modal-open" type="button" class="button small">打开模态框</button>
      <div id="demo-modal" class="reveal-modal">模态对话框正文</div>
    </div>
  </section>
  <script>
    document.getElementById('alert-close').addEventListener('click', (e) => {{
      e.preventDefault();
      document.getElementById('demo-alert').remove();
    }});
    document.getElementById('dd-trigger').addEventListener('click', () => {{
      document.getElementById('dd-menu').classList.toggle('open');
    }});
    document.getElementById('acc-toggle').addEventListener('click', (e) => {{
      e.preventDefault();
      document.getElementById('acc-item').classList.toggle('active');
    }});
    document.getElementById('modal-open').addEventListener('click', () => {{
      document.getElementById('demo-modal').classList.add('open');
    }});
  </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": 480, "height": 900})
        page.set_content(HTML_PAGE)

        # Case 1: hero panel and small button styles
        actual_1 = page.evaluate("""() => {
            const panel = getComputedStyle(document.getElementById('hero-panel'));
            const btn = getComputedStyle(document.getElementById('hero-btn'));
            return {
                panel_bg: panel.backgroundColor,
                panel_padding: panel.paddingTop,
                panel_border: panel.borderTopWidth + ' ' + panel.borderTopStyle + ' ' + panel.borderTopColor,
                btn_bg: btn.backgroundColor,
                btn_font_size: btn.fontSize,
                btn_padding: btn.paddingTop + ' ' + btn.paddingRight
            };
        }""")
        expected_1 = {
            "panel_bg": "rgb(242, 242, 242)",
            "panel_padding": "20px",
            "panel_border": "1px solid rgb(216, 216, 216)",
            "btn_bg": "rgb(0, 140, 186)",
            "btn_font_size": "13px",
            "btn_padding": "14px 28px",
        }
        cases.append({
            "name": "hero_panel_and_small_button",
            "expected": expected_1,
            "actual": actual_1,
            "passed": actual_1 == expected_1,
        })

        # Case 2: mobile-first stacking at 480px vs 3-column horizontal grid at 1024px
        mobile = page.evaluate("""() => {
            const c1 = document.getElementById('col1').getBoundingClientRect();
            const c2 = document.getElementById('col2').getBoundingClientRect();
            const c3 = document.getElementById('col3').getBoundingClientRect();
            return {
                widths: [Math.round(c1.width), Math.round(c2.width), Math.round(c3.width)],
                stacked: c2.top > c1.top && c3.top > c2.top
            };
        }""")
        page.set_viewport_size({"width": 1024, "height": 900})
        desktop = page.evaluate("""() => {
            const row = document.getElementById('grid-row').getBoundingClientRect();
            const c1 = document.getElementById('col1').getBoundingClientRect();
            const c2 = document.getElementById('col2').getBoundingClientRect();
            const c3 = document.getElementById('col3').getBoundingClientRect();
            const pad = getComputedStyle(document.getElementById('col1')).paddingLeft;
            return {
                row_width: Math.round(row.width),
                widths: [Math.round(c1.width), Math.round(c2.width), Math.round(c3.width)],
                same_row: Math.abs(c1.top - c2.top) < 2 && Math.abs(c2.top - c3.top) < 2,
                col_padding_left: pad
            };
        }""")
        actual_2 = {"mobile_480": mobile, "desktop_1024": desktop}
        expected_2 = {
            "mobile_480": {"widths": [480, 480, 480], "stacked": True},
            "desktop_1024": {"row_width": 1000, "widths": [333, 333, 333], "same_row": True, "col_padding_left": "15px"},
        }
        cases.append({
            "name": "mobile_first_grid_breakpoint_switch",
            "expected": expected_2,
            "actual": actual_2,
            "passed": actual_2 == expected_2,
        })

        # Case 3: core visual components (table zebra, progress meter width)
        actual_3 = page.evaluate("""() => {
            const evenBg = getComputedStyle(document.getElementById('tr-even')).backgroundColor;
            const progW = document.getElementById('demo-progress').getBoundingClientRect().width;
            const meterW = document.getElementById('demo-meter').getBoundingClientRect().width;
            return {
                table_even_row_bg: evenBg,
                meter_ratio_pct: Math.round((meterW / (progW - 6)) * 100)
            };
        }""")
        expected_3 = {
            "table_even_row_bg": "rgb(249, 249, 249)",
            "meter_ratio_pct": 65,
        }
        cases.append({
            "name": "component_preview_table_and_progress",
            "expected": expected_3,
            "actual": actual_3,
            "passed": actual_3 == expected_3,
        })

        # Case 4: interactive UI state transitions (dropdown, accordion, modal, alert close)
        before_4 = page.evaluate("""() => ({
            dropdown_display: getComputedStyle(document.getElementById('dd-menu')).display,
            accordion_display: getComputedStyle(document.getElementById('acc-content')).display,
            modal_display: getComputedStyle(document.getElementById('demo-modal')).display,
            alert_exists: !!document.getElementById('demo-alert')
        })""")
        page.click("#dd-trigger")
        page.click("#acc-toggle")
        page.click("#modal-open")
        page.click("#alert-close")
        after_4 = page.evaluate("""() => ({
            dropdown_display: getComputedStyle(document.getElementById('dd-menu')).display,
            accordion_display: getComputedStyle(document.getElementById('acc-content')).display,
            modal_display: getComputedStyle(document.getElementById('demo-modal')).display,
            alert_exists: !!document.getElementById('demo-alert')
        })""")
        actual_4 = {"before": before_4, "after": after_4}
        expected_4 = {
            "before": {"dropdown_display": "none", "accordion_display": "none", "modal_display": "none", "alert_exists": True},
            "after": {"dropdown_display": "block", "accordion_display": "block", "modal_display": "block", "alert_exists": False},
        }
        cases.append({
            "name": "interactive_components_dom_states",
            "expected": expected_4,
            "actual": actual_4,
            "passed": actual_4 == expected_4,
        })

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

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

下载课程关联工程包:Foundation

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

下载课程关联工程包:Foundation

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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