蓝色天空.知识 · 实践 · 创造
我的收藏
jQuery Mobile 教程目录 32章
jQuery Mobile / LEARN BY DOING

jQuery Mobile 工具栏

详解 jQuery Mobile 头部栏(header)与尾部栏(footer)的按钮容量与左右定位类、尾部居中与水平按钮组布局,以及 inline、fixed 与 fixed+fullscreen 三种工具栏定位模式的应用边界。

学完这一章,你能够

  • 详解 jQuery Mobile 头部栏(header)与尾部栏(footer)的按钮容量与左右定位类、尾部居中与水平按钮组布局,以及 inline、fixed 与 fixed+fullscreen 三种工具栏定位模式的应用边界。

头部栏结构与左右按钮定位规则

工具栏通常部署在页面的头部栏(`data-role="header"`)与尾部栏(`data-role="footer"`),为移动端提供触手可及的全局导航。头部栏通常容纳页面标题及最多一至两个按钮(如首页与搜索):当在 `<h1>` 前后各放一个 `<a>` 时,框架会按顺序置于左右两侧;若只在头部放置单个按钮并希望其靠右对齐,仅将其放在 `<h1>` 之后并不足以自动右对齐,必须显式添加 `ui-btn-right` 类(同理左侧可使用 `ui-btn-left` 明确位置)。

尾部栏的多按钮编排与分组居中

相较于头部栏最多两枚按钮的常规约束,尾部栏没有数量限制,适合放置社交分享、快捷操作或分段控制条。默认尾部栏不带额外居中样式与统一间距,可通过在 `data-role="footer"` 上增加 `style="text-align:center;"` 让按钮居中对齐,或进一步嵌套 `<div data-role="controlgroup" data-type="horizontal">` 将多个按钮合并为紧凑的水平按钮组。

工具栏三种定位模式与纯图标快捷栏

通过 `data-position` 与 `data-fullscreen` 可控制工具栏随页面滚动的行为:一是 `inline`(默认),工具栏随文档流正常滚动;二是 `fixed`(`data-position="fixed"`),将头尾工具栏固定在视口顶部和底部;三是 `fullscreen`(需同时声明 `data-position="fixed" data-fullscreen="true"`),工具栏浮盖在照片或视频等沉浸式内容之上。在固定与全屏模式下,点击屏幕即可切换隐藏或重新显示工具栏;另外,在工具栏按钮上使用 `ui-btn-icon-notext` 可呈现紧凑的纯图标栏。现代原生 CSS 中可直接使用 `position: sticky` 或 `position: fixed` 搭配 `env(safe-area-inset-*)` 替代已停更的 jQuery Mobile 固定工具栏。

头部栏与尾部栏按钮容量对比

主题布局差异说明
头部与尾部按钮数量限制头部栏设计容量为 1–2 个操作按钮(左右槽位);尾部栏对按钮数量无硬性上限,可配合居中样式或 controlgroup 自由排布。

运行头尾按钮槽位、尾部按钮组与 inline/fixed/fullscreen 定位离线核验

含完整源码 · 142 行
展开查看收起
python
"""Offline jQuery Mobile DOM & data-role verifier for Chapter 09: toolbars."""
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: Header toolbar button placement (two buttons, left-only, right-only via ui-btn-right)
    doc1 = html.fragment_fromstring(
        """
        <div>
          <div data-role="header" id="h-both">
            <a href="#" class="ui-btn ui-icon-home ui-btn-icon-left">主页</a>
            <h1>标题</h1>
            <a href="#" class="ui-btn ui-icon-search ui-btn-icon-left">搜索</a>
          </div>
          <div data-role="header" id="h-left">
            <a href="#" class="ui-btn ui-btn-left ui-icon-home ui-btn-icon-left">主页</a>
            <h1>标题</h1>
          </div>
          <div data-role="header" id="h-right">
            <h1>标题</h1>
            <a href="#" class="ui-btn ui-btn-right ui-icon-search ui-btn-icon-left">搜索</a>
          </div>
        </div>
        """
    )
    actual_1 = {
        "both_button_count": len(doc1.xpath("./div[@id='h-both']/a")),
        "left_explicit_class": "ui-btn-left" in doc1.xpath("./div[@id='h-left']/a/@class")[0].split(),
        "right_explicit_class": "ui-btn-right" in doc1.xpath("./div[@id='h-right']/a/@class")[0].split(),
        "header_max_buttons": 2,
    }
    expected_1 = {
        "both_button_count": 2,
        "left_explicit_class": True,
        "right_explicit_class": True,
        "header_max_buttons": 2,
    }
    cases.append({
        "name": "header_toolbar_buttons",
        "summary": "Verify header toolbar left/right button limits and ui-btn-left / ui-btn-right placement classes.",
        "expected": expected_1,
        "actual": actual_1,
        "passed": actual_1 == expected_1,
    })

    # Case 2: Footer toolbar multiple buttons, text-align center, and horizontal controlgroup
    doc2 = html.fragment_fromstring(
        """
        <div>
          <div data-role="footer" id="f-plain">
            <a href="#" class="ui-btn ui-icon-plus ui-btn-icon-left">关注渠道一</a>
            <a href="#" class="ui-btn ui-icon-plus ui-btn-icon-left">关注渠道二</a>
            <a href="#" class="ui-btn ui-icon-plus ui-btn-icon-left">关注渠道三</a>
          </div>
          <div data-role="footer" id="f-grouped" style="text-align:center;">
            <div data-role="controlgroup" data-type="horizontal">
              <a href="#" class="ui-btn ui-icon-plus ui-btn-icon-left">操作一</a>
              <a href="#" class="ui-btn ui-icon-plus ui-btn-icon-left">操作二</a>
              <a href="#" class="ui-btn ui-icon-plus ui-btn-icon-left">操作三</a>
            </div>
          </div>
        </div>
        """
    )
    actual_2 = {
        "plain_footer_buttons": len(doc2.xpath("./div[@id='f-plain']/a")),
        "grouped_centered": "text-align:center" in doc2.xpath("./div[@id='f-grouped']/@style")[0],
        "controlgroup_type": doc2.xpath("./div[@id='f-grouped']/div[@data-role='controlgroup']/@data-type")[0],
        "grouped_buttons": len(doc2.xpath("./div[@id='f-grouped']//a")),
    }
    expected_2 = {
        "plain_footer_buttons": 3,
        "grouped_centered": True,
        "controlgroup_type": "horizontal",
        "grouped_buttons": 3,
    }
    cases.append({
        "name": "footer_toolbar_and_controlgroup",
        "summary": "Verify footer unlimited buttons, centered styling, and horizontal controlgroup layout.",
        "expected": expected_2,
        "actual": actual_2,
        "passed": actual_2 == expected_2,
    })

    # Case 3: Toolbar positioning modes (inline, fixed, fixed + fullscreen)
    doc3 = html.fragment_fromstring(
        """
        <div>
          <div data-role="header" data-position="inline"></div>
          <div data-role="footer" data-position="inline"></div>
          <div data-role="header" data-position="fixed"></div>
          <div data-role="footer" data-position="fixed"></div>
          <div data-role="header" data-position="fixed" data-fullscreen="true"></div>
          <div data-role="footer" data-position="fixed" data-fullscreen="true"></div>
        </div>
        """
    )
    bars = doc3.xpath("./div")
    modes = [
        (b.get("data-role"), b.get("data-position"), b.get("data-fullscreen") == "true")
        for b in bars
    ]
    actual_3 = {
        "modes": modes,
        "fullscreen_requires_fixed": all(
            b.get("data-position") == "fixed" for b in bars if b.get("data-fullscreen") == "true"
        ),
    }
    expected_3 = {
        "modes": [
            ("header", "inline", False),
            ("footer", "inline", False),
            ("header", "fixed", False),
            ("footer", "fixed", False),
            ("header", "fixed", True),
            ("footer", "fixed", True),
        ],
        "fullscreen_requires_fixed": True,
    }
    cases.append({
        "name": "toolbar_position_modes",
        "summary": "Verify inline, fixed, and fixed+fullscreen data-position and data-fullscreen attributes on toolbars.",
        "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()
确认头部左右显式定位类、尾部多按钮与水平 controlgroup 居中写法,以及 inline、fixed 与 fixed+fullscreen 六个工具栏属性组合。

查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载本章实验包 (mbskycom202612a2050.zip) ↓ 查看运行说明 ↗

本章附件与实例包

下载课程关联工程包:jQuery Mobile

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

下载课程关联工程包:jQuery Mobile

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

常见错误与边界检查

  • 实际端口、文档资源与应用配置都应和测试条件一致。

技术规范与官方资料

练习与参考解答

查看参考解答

jQuery Mobile 头部栏不会仅因按钮排在 <h1> 后就自动将其放至右侧,需通过 ui-btn-right 明确右槽定位;全屏定位建立在固定定位之上,必须同时配置 data-position="fixed" 与 data-fullscreen="true" 才能生效。

SITE SEARCH

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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