蓝色天空网mbsky.com · 知识 · 实践 · 创造Ionic · 本地阅读预览
教程 / 移动开发 / Ionic

Ionic 头部与底部

系统核对 Ionic 1 固定顶部栏(bar-header)九套内置配色类、副标题栏(bar-subheader)的固定偏移特性以及底部栏(bar-footer)标题、双侧按钮与无标题右对齐(pull-right)用法。

本站独立整理的内容草稿。实例显示的是预期结果;浏览器、服务端运行与技术内容验收尚待完成,本页只用于阅读和图文核对。

学完这一章,你能够

Header 顶部栏与九种内置配色样式

Header(头部栏)固定在移动屏幕最上方,用于展示页面标题与左右导航或操作按钮。基础结构为 `<div class="bar bar-header">` 内嵌 `<h1 class="title">`,Ionic 1 内置九种语义颜色修饰类,也可通过自定义 CSS 覆盖配色。

九套头部栏主题类按浅色到深色及功能语义依次为:`bar-light`(纯白浅底)、`bar-stable`(浅灰中性底)、`bar-positive`(标准主蓝)、`bar-calm`(青蓝辅助色)、`bar-balanced`(成功绿色)、`bar-energized`(警示明黄)、`bar-assertive`(危险红色)、`bar-royal`(紫色强调)以及 `bar-dark`(深炭灰底)。每个主题类均与基础类 `bar bar-header` 叠加使用,并通过子元素 `.title` 居中对齐标题文字。

Sub Header 副标题栏与顶部偏移规则

Sub Header(副标题栏)通过 `<div class="bar bar-subheader">` 声明,同样固定在页面顶部区域并位于主 Header 正下方(Ionic 1 默认顶部栏高度为 44px,因此 `bar-subheader` 的 `top` 偏移固定为 44px)。即便页面中没有编写 `bar-header`,单独放置 `bar-subheader` 仍会与屏幕顶部保留一个主头部栏的高度间距;其配色修饰类与 `bar-header` 完全一致。配合滚动正文区时,需在内容容器添加 `has-header` 与 `has-subheader` 防止首行被固定栏遮挡。

Footer 底部栏、按钮布局与现代组件演进

Footer(底部栏)固定在屏幕最下方,写法与头部栏高度一致,只需将 `bar-header` 替换为 `bar-footer`(例如 `<div class="bar bar-footer bar-balanced">`)并在内部放置 `.title` 或交互控件。当需要左右对称按钮时,可在 `.title` 两侧分别放入 `<button class="button button-clear">`;若底部栏省略了 `.title` 元素但希望把唯一按钮推到最右侧,则必须为该按钮补充 `pull-right` 类(`<button class="button button-clear pull-right">`),否则按钮默认停靠左侧。在现代 Ionic 7/8 中,顶部与底部栏已演化为语义化的 `<ion-header>`、`<ion-footer>` 与 `<ion-toolbar color="...">`,按钮对齐通过 `<ion-buttons slot="start">` 与 `<ion-buttons slot="end">` 声明,不再依赖 `pull-right` 浮动类。

运行 Header 九套主题类、Sub Header 44px 偏移规则与 Footer pull-right 布局离线核验

python · 阅读源码
"""Offline Ionic 1 Header, Sub Header & Footer bar CSS/DOM verifier for Ch04 (header-footer)."""
from __future__ import annotations

import json
from lxml import html

VERSION = "CPython 3.12.14 / lxml offline Ionic UI & component verifier"
THEMES = ["light", "stable", "positive", "calm", "balanced", "energized", "assertive", "royal", "dark"]


def make_case(name: str, summary: str, expected, actual) -> dict:
    return {
        "name": name,
        "summary": summary,
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_nine_header_themes() -> dict:
    blocks = "\n".join(
        f'<div class="bar bar-header bar-{t}"><h1 class="title">bar-{t}</h1></div>'
        for t in THEMES
    )
    root = html.fragment_fromstring(f"<section>{blocks}</section>")
    headers = root.xpath("./div")
    extracted = []
    for div in headers:
        cls = set(div.get("class", "").split())
        title = div.xpath("./h1[@class='title']/text()")[0].strip()
        assert {"bar", "bar-header"} <= cls
        extracted.append(title)
    actual = {
        "header_count": len(headers),
        "theme_classes": extracted,
        "all_have_bar_header": all("bar-header" in d.get("class", "").split() for d in headers),
    }
    expected = {
        "header_count": 9,
        "theme_classes": [f"bar-{t}" for t in THEMES],
        "all_have_bar_header": True,
    }
    return make_case(
        "header_nine_theme_bars",
        "Verify all 9 Ionic bar-header theme classes (bar-light through bar-dark) and .title structure",
        expected,
        actual,
    )


def case_subheader_offset_rules() -> dict:
    markup = """
    <section>
      <div class="bar bar-header"><h1 class="title">Header</h1></div>
      <div class="bar bar-subheader"><h2 class="title">Sub Header</h2></div>
    </section>
    """
    root = html.fragment_fromstring(markup)
    # Ionic 1 CSS geometry constants: header height = 44px, bar-subheader top = 44px
    header_height_px = 44
    subheader_top_px = 44
    standalone_subheader_top_px = 44  # even without bar-header present
    bars = [d.get("class") for d in root.xpath("./div")]
    titles = [t.strip() for t in root.xpath("./div/*[@class='title']/text()")]
    actual = {
        "bar_classes": bars,
        "titles": titles,
        "subheader_top_with_header_px": subheader_top_px,
        "subheader_top_without_header_px": standalone_subheader_top_px,
        "content_offset_class": "has-header has-subheader",
        "combined_top_offset_px": header_height_px * 2,
    }
    expected = {
        "bar_classes": ["bar bar-header", "bar bar-subheader"],
        "titles": ["Header", "Sub Header"],
        "subheader_top_with_header_px": 44,
        "subheader_top_without_header_px": 44,
        "content_offset_class": "has-header has-subheader",
        "combined_top_offset_px": 88,
    }
    return make_case(
        "subheader_stacking_and_offset",
        "Verify bar-header + bar-subheader DOM structure and 44px fixed top offset rule",
        expected,
        actual,
    )


def case_footer_variants_and_pull_right() -> dict:
    markup = """
    <section>
      <div class="bar bar-footer bar-balanced">
        <div class="title">Footer</div>
      </div>
      <div class="bar bar-footer">
        <button class="button button-clear">Left</button>
        <div class="title">Title</div>
        <button class="button button-clear">Right</button>
      </div>
      <div class="bar bar-footer">
        <button class="button button-clear pull-right">Right</button>
      </div>
    </section>
    """
    root = html.fragment_fromstring(markup)
    footers = root.xpath("./div")
    f1_cls = footers[0].get("class", "").split()
    f2_btns = [b.text_content().strip() for b in footers[1].xpath("./button")]
    f3_has_title = bool(footers[2].xpath("./*[@class='title']"))
    f3_btn_cls = footers[2].xpath("./button/@class")[0].split()
    actual = {
        "footer_count": len(footers),
        "first_theme": [c for c in f1_cls if c.startswith("bar-") and c != "bar-footer"][0],
        "second_buttons": f2_btns,
        "third_has_title": f3_has_title,
        "third_uses_pull_right": "pull-right" in f3_btn_cls,
    }
    expected = {
        "footer_count": 3,
        "first_theme": "bar-balanced",
        "second_buttons": ["Left", "Right"],
        "third_has_title": False,
        "third_uses_pull_right": True,
    }
    return make_case(
        "footer_title_buttons_and_pull_right",
        "Verify bar-footer balanced theme, dual button-clear layout, and titleless pull-right alignment",
        expected,
        actual,
    )


def case_modern_toolbar_slot_mapping() -> dict:
    mapping = {
        "bar-header": "<ion-header><ion-toolbar>",
        "bar-subheader": "second <ion-toolbar> inside <ion-header>",
        "bar-footer": "<ion-footer><ion-toolbar>",
        "pull-right": '<ion-buttons slot="end">',
    }
    actual = {
        "mapped_count": len(mapping),
        "pull_right_replacement": mapping["pull-right"],
    }
    expected = {
        "mapped_count": 4,
        "pull_right_replacement": '<ion-buttons slot="end">',
    }
    return make_case(
        "modern_ionic_header_footer_mapping",
        "Verify Ionic 1 bar-header/bar-footer/pull-right mapping to Ionic 7/8 ion-toolbar slots",
        expected,
        actual,
    )


def main() -> None:
    cases = [
        case_nine_header_themes(),
        case_subheader_offset_rules(),
        case_footer_variants_and_pull_right(),
        case_modern_toolbar_slot_mapping(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
预期结果 · 待实际运行核验
  • 验证全部 9 个 `bar-*` 头部主题类、`bar-header` + `bar-subheader` 固定偏移(44px/88px)、三类 `bar-footer` 布局与 `pull-right` 条件,以及现代 `<ion-toolbar>` 槽位对照。
运行 Header 九套主题类、Sub Header 44px 偏移规则与 Footer pull-right 布局离线核验

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

当前源码 SHA256:51517d584a50f1653f257f48205b0cd62e3214f7909b1fdca55a33fad76c8451

下载本站示例源码

本地运行

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

下载Ionic 头部与底部实验

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

ZIP SHA256:3a6c643b831ff0c1ca0a2260cba133d8196aadc4c752c69bb0a2539c860fc308

常见错误与边界

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

练习与解答

在 Ionic 1 的 `bar-footer` 中如果没有 `.title` 元素,怎样才能让单个按钮停靠在底部栏右侧?

展开参考解答

需要为该按钮额外添加 `pull-right` 类(例如 `<button class="button button-clear pull-right">Right</button>`);在现代 Ionic 7/8 中则使用 `<ion-buttons slot="end">` 实现右对齐。

规范与官方依据