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

离线验证 Foundation 5 Off-Canvas 左侧抽屉滑入、遮罩关闭与工具栏样式

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

FOUNDATION_CSS = """
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 16px; color: #222; background: #fff; }
.off-canvas-wrap { position: relative; width: 100%; overflow: hidden; min-height: 360px; }
.inner-wrap { position: relative; width: 100%; min-height: 360px; transform: translate3d(0, 0, 0); transition: none; }
.off-canvas-wrap.move-right > .inner-wrap { transform: translate3d(250px, 0, 0); }
.tab-bar { background: #333333; color: #ffffff; height: 45px; line-height: 45px; position: relative; }
.tab-bar .left-small { width: 45px; height: 45px; position: absolute; top: 0; left: 0; border-right: 1px solid #1a1a1a; }
.tab-bar .menu-icon { display: block; width: 45px; height: 45px; position: relative; text-indent: -9999px; color: #fff; }
.tab-bar .menu-icon span::after { content: ""; position: absolute; display: block; height: 0; top: 50%; margin-top: -8px; left: 14px; box-shadow: 0 0 0 1px #fff, 0 7px 0 1px #fff, 0 14px 0 1px #fff; width: 16px; }
.tab-bar-section.middle { position: absolute; top: 0; left: 45px; right: 45px; height: 45px; padding: 0 14px; text-align: center; }
.tab-bar .title { margin: 0; font-size: 18px; font-weight: 700; line-height: 45px; color: #ffffff; }
.left-off-canvas-menu { width: 250px; top: 0; bottom: 0; position: absolute; overflow-y: auto; background: #333333; z-index: 1001; transform: translate3d(-100%, 0, 0); }
ul.off-canvas-list { list-style-type: none; padding: 0; margin: 0; }
ul.off-canvas-list li label { display: block; padding: 5px 15px; color: #999999; text-transform: uppercase; font-size: 12px; font-weight: 700; background: #444444; border-top: 1px solid #5e5e5e; margin: 0; }
ul.off-canvas-list li a { display: block; padding: 10px 15px; color: rgba(255, 255, 255, 0.7); border-bottom: 1px solid #262626; text-decoration: none; }
.main-section { padding: 20px; background: #ffffff; min-height: 315px; }
.exit-off-canvas { display: none; position: absolute; top: 0; bottom: 0; left: 0; right: 0; z-index: 1002; background: rgba(255, 255, 255, 0.2); cursor: pointer; }
.off-canvas-wrap.move-right .exit-off-canvas { display: block; }
"""

HTML_PAGE = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><style>{FOUNDATION_CSS}</style></head>
<body>
  <div id="wrap" class="off-canvas-wrap" data-offcanvas>
    <div id="inner" class="inner-wrap">
      <nav class="tab-bar">
        <section class="left-small">
          <a id="toggle" class="left-off-canvas-toggle menu-icon" href="#"><span></span></a>
        </section>
        <section class="middle tab-bar-section">
          <h1 id="bar-title" class="title">Off-canvas Example</h1>
        </section>
      </nav>
      <aside id="left-menu" class="left-off-canvas-menu">
        <ul class="off-canvas-list test">
          <li><label id="menu-label">Heading</label></li>
          <li><a href="#l1">Link 1</a></li>
          <li><a href="#l2">Link 2</a></li>
          <li><a href="#l3">Link 3</a></li>
        </ul>
      </aside>
      <section id="main" class="main-section">
        <h3>主内容区域</h3>
        <p>点击左上角菜单图标可使左侧抽屉菜单滑入,点击遮罩层可平滑关闭。</p>
      </section>
      <a id="exit" class="exit-off-canvas"></a>
    </div>
  </div>
  <script>
    (function initFoundationOffCanvas() {{
      document.querySelectorAll('[data-offcanvas]').forEach(function(wrap) {{
        wrap.querySelectorAll('.left-off-canvas-toggle').forEach(function(btn) {{
          btn.addEventListener('click', function(e) {{
            e.preventDefault();
            wrap.classList.toggle('move-right');
          }});
        }});
        var exit = wrap.querySelector('.exit-off-canvas');
        if (exit) {{
          exit.addEventListener('click', function(e) {{
            e.preventDefault();
            wrap.classList.remove('move-right');
          }});
        }}
      }});
    }})();
  </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": 640, "height": 500})
        page.set_content(HTML_PAGE)

        # Case 1: initial hidden off-canvas structure
        init = page.evaluate("""() => {
            const wrap = document.getElementById('wrap');
            const bar = document.querySelector('.tab-bar');
            const menu = document.getElementById('left-menu');
            const exit = document.getElementById('exit');
            return {
                has_data_offcanvas: wrap.hasAttribute('data-offcanvas'),
                wrap_overflow: getComputedStyle(wrap).overflow,
                tab_bar_height: Math.round(bar.getBoundingClientRect().height),
                tab_bar_bg: getComputedStyle(bar).backgroundColor,
                menu_width: Math.round(menu.getBoundingClientRect().width),
                menu_left_initial: Math.round(menu.getBoundingClientRect().left),
                exit_display_initial: getComputedStyle(exit).display,
                link_count: document.querySelectorAll('.off-canvas-list li a').length,
            };
        }""")
        exp1 = {
            "has_data_offcanvas": True,
            "wrap_overflow": "hidden",
            "tab_bar_height": 45,
            "tab_bar_bg": "rgb(51, 51, 51)",
            "menu_width": 250,
            "menu_left_initial": -250,
            "exit_display_initial": "none",
            "link_count": 3,
        }
        cases.append(dict(name="offcanvas_initial_structure_and_hidden_menu", passed=(init == exp1), expected=exp1, actual=init))

        # Case 2: toggle open -> move-right slides inner-wrap 250px right
        page.click("#toggle")
        opened = page.evaluate("""() => {
            const wrap = document.getElementById('wrap');
            const inner = document.getElementById('inner');
            const menu = document.getElementById('left-menu');
            const main = document.getElementById('main');
            const exit = document.getElementById('exit');
            return {
                has_move_right: wrap.classList.contains('move-right'),
                inner_transform: getComputedStyle(inner).transform,
                menu_left_open: Math.round(menu.getBoundingClientRect().left),
                main_left_open: Math.round(main.getBoundingClientRect().left),
                exit_display_open: getComputedStyle(exit).display,
            };
        }""")
        exp2 = {
            "has_move_right": True,
            "inner_transform": "matrix(1, 0, 0, 1, 250, 0)",
            "menu_left_open": 0,
            "main_left_open": 250,
            "exit_display_open": "block",
        }
        cases.append(dict(name="offcanvas_toggle_move_right_slide", passed=(opened == exp2), expected=exp2, actual=opened))

        # Case 3: click exit-off-canvas closes drawer
        page.click("#exit")
        closed = page.evaluate("""() => {
            const wrap = document.getElementById('wrap');
            const menu = document.getElementById('left-menu');
            const main = document.getElementById('main');
            const exit = document.getElementById('exit');
            return {
                has_move_right: wrap.classList.contains('move-right'),
                menu_left_closed: Math.round(menu.getBoundingClientRect().left),
                main_left_closed: Math.round(main.getBoundingClientRect().left),
                exit_display_closed: getComputedStyle(exit).display,
            };
        }""")
        exp3 = {
            "has_move_right": False,
            "menu_left_closed": -250,
            "main_left_closed": 0,
            "exit_display_closed": "none",
        }
        cases.append(dict(name="offcanvas_exit_overlay_closes_menu", passed=(closed == exp3), expected=exp3, actual=closed))

        # Case 4: typography and list label styles
        typo = page.evaluate("""() => {
            const title = document.getElementById('bar-title');
            const lbl = document.getElementById('menu-label');
            const ts = getComputedStyle(title);
            const ls = getComputedStyle(lbl);
            return {
                title_text: title.textContent,
                title_color: ts.color,
                label_transform: ls.textTransform,
                label_bg: ls.backgroundColor,
                label_weight: ls.fontWeight,
            };
        }""")
        exp4 = {
            "title_text": "Off-canvas Example",
            "title_color": "rgb(255, 255, 255)",
            "label_transform": "uppercase",
            "label_bg": "rgb(68, 68, 68)",
            "label_weight": "700",
        }
        cases.append(dict(name="offcanvas_list_and_tab_bar_typography", passed=(typo == exp4), expected=exp4, actual=typo))

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

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

下载课程关联工程包:Foundation

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

下载课程关联工程包:Foundation

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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