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

离线实测 Foundation 5 中型设备 .medium-6 组合覆盖、向上继承与 640/641px 边界

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

FOUNDATION_GRID_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
body { margin: 0; font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; }
.row { width: 100%; margin: 0 auto; max-width: 62.5rem; }
.row::before, .row::after { content: " "; display: table; }
.row::after { clear: both; }
.column, .columns { padding-left: 0.9375rem; padding-right: 0.9375rem; width: 100%; float: left; position: relative; }
.small-3 { width: 25%; }
.small-9 { width: 75%; }
@media only screen and (min-width: 40.0625em) {
  .medium-6 { width: 50%; }
}
.demo-col { min-height: 28px; }
"""

HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{FOUNDATION_GRID_CSS}</style></head>
<body>
  <div id="row-combo" class="row">
    <div id="c-left" class="small-3 medium-6 columns demo-col" style="background-color:yellow;"><p>左栏</p></div>
    <div id="c-right" class="small-9 medium-6 columns demo-col" style="background-color:pink;"><p>右栏</p></div>
  </div>
  <div id="row-med-only" class="row">
    <div id="mo-left" class="medium-6 columns demo-col" style="background-color:yellow;"><p>仅中屏左</p></div>
    <div id="mo-right" class="medium-6 columns demo-col" style="background-color:pink;"><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": 375, "height": 800})
        page.set_content(HTML_PAGE)

        # Case 1: .small-3/.small-9 combined with .medium-6/.medium-6 across 375, 768, 1200px
        actual_1 = {}
        for vw in (375, 768, 1200):
            page.set_viewport_size({"width": vw, "height": 800})
            actual_1[str(vw)] = page.evaluate("""() => {
                const a = document.getElementById('c-left').getBoundingClientRect();
                const b = document.getElementById('c-right').getBoundingClientRect();
                return {
                    widths: [Math.round(a.width * 10) / 10, Math.round(b.width * 10) / 10],
                    same_row: Math.abs(a.top - b.top) < 1,
                };
            }""")
        expected_1 = {
            "375": {"widths": [93.8, 281.3], "same_row": True},
            "768": {"widths": [384.0, 384.0], "same_row": True},
            "1200": {"widths": [500.0, 500.0], "same_row": True},
        }
        cases.append({
            "name": "small_3_9_combined_with_medium_6_6",
            "expected": expected_1,
            "actual": actual_1,
            "passed": actual_1 == expected_1,
        })

        # Case 2: Medium-only (.medium-6 without .small-*) stacks at 375px, 50/50 at 768px and 1200px
        actual_2 = {}
        for vw in (375, 768, 1200):
            page.set_viewport_size({"width": vw, "height": 800})
            actual_2[str(vw)] = page.evaluate("""() => {
                const a = document.getElementById('mo-left').getBoundingClientRect();
                const b = document.getElementById('mo-right').getBoundingClientRect();
                return {
                    widths: [Math.round(a.width * 10) / 10, Math.round(b.width * 10) / 10],
                    same_row: Math.abs(a.top - b.top) < 1,
                    stacked: b.top >= a.bottom - 1,
                };
            }""")
        expected_2 = {
            "375": {"widths": [375.0, 375.0], "same_row": False, "stacked": True},
            "768": {"widths": [384.0, 384.0], "same_row": True, "stacked": False},
            "1200": {"widths": [500.0, 500.0], "same_row": True, "stacked": False},
        }
        cases.append({
            "name": "medium_only_stacks_on_small_splits_on_medium_and_large",
            "expected": expected_2,
            "actual": actual_2,
            "passed": actual_2 == expected_2,
        })

        # Case 3: Exact medium range boundaries (640 vs 641, 1024 vs 1025)
        actual_3 = {}
        for vw in (640, 641, 1024, 1025):
            page.set_viewport_size({"width": vw, "height": 800})
            actual_3[str(vw)] = page.evaluate("""() => {
                const a = document.getElementById('c-left').getBoundingClientRect();
                const b = document.getElementById('c-right').getBoundingClientRect();
                const m1 = document.getElementById('mo-left').getBoundingClientRect();
                const m2 = document.getElementById('mo-right').getBoundingClientRect();
                return {
                    combo_widths: [Math.round(a.width * 10) / 10, Math.round(b.width * 10) / 10],
                    med_only_same_row: Math.abs(m1.top - m2.top) < 1,
                };
            }""")
        expected_3 = {
            "640": {"combo_widths": [160.0, 480.0], "med_only_same_row": False},
            "641": {"combo_widths": [320.5, 320.5], "med_only_same_row": True},
            "1024": {"combo_widths": [500.0, 500.0], "med_only_same_row": True},
            "1025": {"combo_widths": [500.0, 500.0], "med_only_same_row": True},
        }
        cases.append({
            "name": "medium_breakpoint_range_boundaries",
            "expected": expected_3,
            "actual": actual_3,
            "passed": actual_3 == expected_3,
        })

        browser.close()
    print(json.dumps({"cases": cases}, ensure_ascii=False))


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

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

入口文件:projects/foundation-grid-medium-lab/explore-grid-medium.py

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

下载课程关联工程包:Foundation

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

下载课程关联工程包:Foundation

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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