jQuery Mobile 面板
系统梳理 jQuery Mobile 侧滑抽屉面板(data-role="panel")在页面 DOM 中的兄弟节点位置约束、关闭方式(外部点击/滑动/data-rel="close")、三种展示动画(overlay/push/reveal)以及右侧定位与固定滚动配置。
学完这一章,你能够
- 系统梳理 jQuery Mobile 侧滑抽屉面板(data-role="panel")在页面 DOM 中的兄弟节点位置约束、关闭方式(外部点击/滑动/data-rel="close")、三种展示动画(overlay/push/reveal)以及右侧定位与固定滚动配置。
面板结构与页面 DOM 放置约束
jQuery Mobile 面板(Panel)即移动端常见的侧滑抽屉菜单,默认从屏幕左侧滑出。创建面板只需在 `data-role="page"` 内部声明带唯一 `id` 的 `<div data-role="panel" id="myPanel">` 并在其中放入标题、菜单或文本标记。关键结构约束在于:面板容器必须作为直接子元素放置在页面 `header`、`main`、`footer` 之前或之后,而不能嵌套在 `main` 内部;随后在页面任意位置添加指向 `#myPanel` 的链接按钮即可点击展开面板。
关闭控制属性与显式关闭按钮
默认情况下,用户点击面板外部遮罩区域、按下 `Esc` 键或在屏幕上反向滑动均可关闭面板。通过设置 `data-dismissible="false"` 可禁用外部点击关闭,通过 `data-swipe-close="false"` 可禁用滑动手势关闭。若需要在面板内部放置关闭按钮,给按钮加上 `data-rel="close"`,并建议将 `href` 指向所属页面的 `#pageId`(如 `href="#pageone"`)以兼顾性能与无脚本回退。
三种展示模式与侧边/固定定位
借助 `data-display` 属性可控制面板与页面主体的相对运动方式:`reveal`(默认)将主页面滑开露出下方固定面板,`overlay` 让面板直接浮盖在主页面上方,`push` 则让面板与主页面同步推移。默认面板靠左展示并随页面整体滚动;添加 `data-position="right"` 可改为右侧滑出,添加 `data-position-fixed="true"` 则使面板固定于视口不随长页面滚走(注意面板内容高度超出视口时仍应谨慎评估滚动可用性)。在现代移动端 Web 开发中,此类抽屉菜单通常使用原生 `<dialog>`、CSS `transform: translateX()` 与焦点管理脚本替代已停更的 jQuery Mobile 面板。
面板关闭行为控制属性表
| 属性名 | 可选值 | 功能描述 | 离线核验 |
| data-dismissible | true | false | 控制是否允许点击面板外部区域关闭面板(默认 true)。 | panel_dismiss_and_swipe_controls |
| data-swipe-close | true | false | 控制是否允许通过滑动手势关闭面板(默认 true)。 | panel_dismiss_and_swipe_controls |
data-display 面板展示模式表
| 属性取值 | 视觉交互效果 |
| data-display="overlay" | 面板直接覆盖在当前页面内容之上,主页面保持不动。 |
| data-display="push" | 面板滑入的同时将主页面同步向另一侧推移。 |
| data-display="reveal" | 默认值;主页面像幕布一样侧滑移开,露出位于底层的面板。 |
运行面板兄弟位置、关闭开关与 overlay/reveal/push 展示模式离线核验
展开查看收起
"""Offline jQuery Mobile DOM & data-role verifier for Chapter 11: panels."""
import json
from lxml import html
VERSION = "CPython 3.12.14 / lxml offline jQuery Mobile DOM & data-role verifier"
def run_cases():
cases = []
# Case 1: Panel placement inside page (sibling before/after header/main/footer) & trigger link
doc1 = html.fragment_fromstring(
"""
<div data-role="page" id="pageone">
<div data-role="panel" id="myPanel">
<h2>面板头部</h2>
<p>面板内容</p>
<a href="#pageone" data-rel="close" class="ui-btn ui-btn-inline">关闭面板</a>
</div>
<div data-role="header"><h1>标准页面头部</h1></div>
<div data-role="main" class="ui-content">
<p>点击下面按钮打开面板。</p>
<a href="#myPanel" class="ui-btn ui-btn-inline">打开面板</a>
</div>
<div data-role="footer"><h1>底部文本</h1></div>
</div>
"""
)
child_roles = [el.get("data-role") for el in doc1.xpath("./div[@data-role]")]
open_href = doc1.xpath("./div[@data-role='main']//a/@href")[0]
close_btn = doc1.xpath("./div[@data-role='panel']//a[@data-rel='close']")[0]
actual_1 = {
"child_roles": child_roles,
"panel_before_header": child_roles.index("panel") == 0,
"open_matches_panel_id": open_href == "#" + doc1.xpath("./div[@data-role='panel']/@id")[0],
"close_href_points_to_page": close_btn.get("href") == "#" + doc1.get("id"),
}
expected_1 = {
"child_roles": ["panel", "header", "main", "footer"],
"panel_before_header": True,
"open_matches_panel_id": True,
"close_href_points_to_page": True,
}
cases.append({
"name": "panel_page_structure_and_close_btn",
"summary": "Verify data-role='panel' sibling placement inside page, open anchor #id match, and data-rel='close' return href.",
"expected": expected_1,
"actual": actual_1,
"passed": actual_1 == expected_1,
})
# Case 2: Dismiss controls (data-dismissible & data-swipe-close)
doc2 = html.fragment_fromstring(
"""
<div>
<div data-role="panel" id="p-default"></div>
<div data-role="panel" id="p-nodismiss" data-dismissible="false"></div>
<div data-role="panel" id="p-noswipe" data-swipe-close="false"></div>
</div>
"""
)
p_def, p_nodis, p_noswp = doc2.xpath("./div[@data-role='panel']")
actual_2 = {
"default_dismissible": p_def.get("data-dismissible", "true"),
"default_swipe_close": p_def.get("data-swipe-close", "true"),
"disabled_dismissible": p_nodis.get("data-dismissible"),
"disabled_swipe_close": p_noswp.get("data-swipe-close"),
}
expected_2 = {
"default_dismissible": "true",
"default_swipe_close": "true",
"disabled_dismissible": "false",
"disabled_swipe_close": "false",
}
cases.append({
"name": "panel_dismiss_and_swipe_controls",
"summary": "Verify data-dismissible and data-swipe-close defaults ('true') and explicit 'false' overrides.",
"expected": expected_2,
"actual": actual_2,
"passed": actual_2 == expected_2,
})
# Case 3: Display modes (overlay / reveal / push) and positioning (right & position-fixed)
doc3 = html.fragment_fromstring(
"""
<div>
<div data-role="panel" id="overlayPanel" data-display="overlay"></div>
<div data-role="panel" id="revealPanel" data-display="reveal"></div>
<div data-role="panel" id="pushPanel" data-display="push"></div>
<div data-role="panel" id="rightPanel" data-position="right"></div>
<div data-role="panel" id="fixedPanel" data-position-fixed="true"></div>
</div>
"""
)
actual_3 = {
"display_modes": doc3.xpath("./div[@data-display]/@data-display"),
"right_position": doc3.xpath("./div[@id='rightPanel']/@data-position")[0],
"position_fixed": doc3.xpath("./div[@id='fixedPanel']/@data-position-fixed")[0] == "true",
"default_display": "reveal",
"default_position": "left",
}
expected_3 = {
"display_modes": ["overlay", "reveal", "push"],
"right_position": "right",
"position_fixed": True,
"default_display": "reveal",
"default_position": "left",
}
cases.append({
"name": "panel_display_and_position_options",
"summary": "Verify data-display (overlay/reveal/push), data-position='right', and data-position-fixed='true'.",
"expected": expected_3,
"actual": actual_3,
"passed": actual_3 == expected_3,
})
return cases
def main():
print(json.dumps({"version": VERSION, "cases": run_cases()}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
面板需要作为页面级侧边容器与页面主体(header/main/footer 包装层)协同位移动画,因此必须置于页面直接子级(header 之前或 footer 之后);reveal 滑开页面露出底层面板,overlay 让面板浮盖在页面上方,push 则同步推动面板与页面。