Foundation 滑动导航(Off-Canvas)
核对 Foundation 5 Off-Canvas 侧滑抽屉导航的层级结构、.move-right 位移过渡、.exit-off-canvas 遮罩关闭与 Foundation JS 初始化流程。
学完这一章,你能够
- 核对 Foundation 5 Off-Canvas 侧滑抽屉导航的层级结构、.move-right 位移过渡、.exit-off-canvas 遮罩关闭与 Foundation JS 初始化流程。
移动端侧边栏滑动导航结构
Off-Canvas 滑动导航是移动端常用的抽屉式侧边栏方案:导航菜单默认隐藏在视口外侧,点击顶部工具栏中的菜单图标后从屏幕左侧平推滑出,既节省窄屏可视空间,又保持主内容区完整可读。
创建 Off-Canvas 滑动导航与交互实测
构建左侧滑动导航需严格嵌套五个核心节点:最外层 `.off-canvas-wrap` 声明 `data-offcanvas` 并裁剪溢出;内部 `.inner-wrap` 包裹顶部 `.tab-bar`(含 `.left-small > a.left-off-canvas-toggle.menu-icon` 触发钮与 `.middle.tab-bar-section` 标题栏)、左侧抽屉 `<aside class="left-off-canvas-menu">`(内置 `ul.off-canvas-list` 与 `<label>` 分组标题)、主体 `<section class="main-section">` 以及关闭遮罩 `<a class="exit-off-canvas">`。页面就绪后需调用 `$(document).foundation();` 完成事件绑定。
本站离线 Playwright Chromium 实测(`640×500` 视口)确认:初始状态下 `.off-canvas-wrap` 计算样式为 `overflow: hidden`,`.tab-bar` 高 `45px`、深灰背景 `rgb(51, 51, 51)`,宽 `250px` 的左侧抽屉停在视口外 `left: -250px`,`.exit-off-canvas` 处于 `display: none`;点击 `.left-off-canvas-toggle` 后容器添加 `.move-right`,`.inner-wrap` 施加 `matrix(1, 0, 0, 1, 250, 0)` 平移,抽屉左缘移至 `0px`、主内容区右推至 `250px` 且遮罩转为 `display: block`;点击遮罩后 `.move-right` 移除,抽屉回退至 `-250px`。菜单分组 `<label>` 实测呈现大写加粗(`fontWeight: 700`、`textTransform: uppercase`、背景 `rgb(68, 68, 68)`)。
离线验证 Foundation 5 Off-Canvas 左侧抽屉滑入、遮罩关闭与工具栏样式
"""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()
- 在离线 Chromium 中核对抽屉初始左偏移 -250px、点击触发后 .move-right 平移 250px 显露菜单与遮罩、点击 .exit-off-canvas 复位及工具栏与列表标签排版。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
当前源码 SHA256:40bfbbcdb915f805e82af97bdefb0c4219812483141e84580fdb2a2fa986e4be
本地运行
解压后按 README 说明在对应本地环境运行脚本。
所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
ZIP SHA256:2108b283c4b97c15b4d5007570885242f0db75fb11bd821a4faa617a86ab47a4
常见错误与边界
- 实际端口、文档资源与应用配置都应和测试条件一致。
练习与解答
Foundation 5 的 Off-Canvas 导航中,.exit-off-canvas 元素起什么作用?为什么必须保留 .off-canvas-wrap 与 .inner-wrap 两层容器?
展开参考解答
.exit-off-canvas 在抽屉展开时铺满主内容区作为透明遮罩,点击它即可收起侧边栏;.off-canvas-wrap 负责 overflow: hidden 裁剪隐藏超出视口的抽屉,而 .inner-wrap 则承担整体 translate3d 水平位移,两者缺一不可。