ionic 头部和底部
拆解 Ionic 1 `<ion-header-bar>` 与 `<ion-footer-bar>` 固定栏指令的按钮布局、副标题栏(`bar-subheader`)、平台默认标题对齐规则(`align-title`)与点顶滚回控制(`no-tap-scroll`),并说明 Ionic 7/8 迁移写法。
学完这一章,你能够
- 拆解 Ionic 1 `<ion-header-bar>` 与 `<ion-footer-bar>` 固定栏指令的按钮布局、副标题栏(`bar-subheader`)、平台默认标题对齐规则(`align-title`)与点顶滚回控制(`no-tap-scroll`),并说明 Ionic 7/8 迁移写法。
ion-header-bar 顶部固定标题栏与 API
`<ion-header-bar>` 用于在视口顶部固定渲染一个主标题栏,若为其附加 `bar-subheader` 样式类则可作为紧贴主栏下方的副标题栏使用。栏内通常按左按钮组(`<div class="buttons">`)、标题(`<h1 class="title">`)、右按钮组的顺序排布,紧随其后的 `<ion-content>` 会自动避让头部高度。
`<ion-header-bar>` 提供两个核心可选属性:`align-title` 控制标题对齐方向(`'left'`、`'center'` 或 `'right'`),省略时按宿主平台规范自动决定——iOS 默认居中、Android 默认居左;`no-tap-scroll` 为布尔开关,因为默认点击顶部标题栏会触发页面内容滚回顶部,设为 `true` 即可关闭该快捷回顶行为。
ion-footer-bar 底部固定栏与现代迁移
`<ion-footer-bar>` 与头部栏结构对称,固定停靠在屏幕底部,同样支持 `<div class="buttons">` 与 `<h1 class="title">` 组合以及主题色类(如 `bar-assertive`)。在 API 方面,`<ion-footer-bar>` 仅保留 `align-title`(默认行为同为 iOS 居中、Android 居左),不包含点顶滚回属性。迁移到现代 Ionic 7/8 时,顶部与底部容器升级为 `<ion-header>` 与 `<ion-footer>`,内部嵌套 `<ion-toolbar color="primary">`、`<ion-buttons slot="start|end">` 与 `<ion-title>`,并原生适配刘海屏与底部安全区(Safe Area)。
ion-header-bar 属性 API
| 属性 | 类型 | 描述 |
| align-title (可选) | string | 控制标题对齐方式('left'、'center'、'right');缺省时 iOS 默认居中、Android 默认居左。 |
| no-tap-scroll (可选) | boolean | 默认点击头部标题栏会将内容滚回顶部,设为 true 可禁用该回滚动作。 |
ion-footer-bar 属性 API
| 属性 | 类型 | 描述 |
| align-title (可选) | string | 控制底部栏标题对齐方式('left'、'center'、'right');缺省时遵循平台默认排版。 |
运行 ion-header-bar / ion-footer-bar 结构解析、平台默认对齐与 no-tap-scroll 离线实测
展开查看收起
"""Offline Ionic 1 ion-header-bar & ion-footer-bar directive verifier."""
import json
from lxml import html
def resolve_title_align(attr_value, platform: str) -> str:
if attr_value in {"left", "center", "right"}:
return attr_value
return "center" if platform == "ios" else "left"
def simulate_header_tap(scroll_y: int, no_tap_scroll: bool) -> int:
return scroll_y if no_tap_scroll else 0
def main():
cases = []
# Case 1: Parse ion-header-bar + bar-subheader + ion-content
hdr_markup = """
<div>
<ion-header-bar align-title="left" class="bar-positive">
<div class="buttons"><button class="button" ng-click="doSomething()">Left Button</button></div>
<h1 class="title">Title!</h1>
<div class="buttons"><button class="button">Right Button</button></div>
</ion-header-bar>
<ion-header-bar class="bar-subheader bar-light">
<h2 class="title">Subheader</h2>
</ion-header-bar>
<ion-content>Some content!</ion-content>
</div>
"""
doc = html.fromstring(hdr_markup)
bars = doc.xpath("//ion-header-bar")
actual_hdr = {
"bar_count": len(bars),
"primary_align": bars[0].get("align-title"),
"primary_class": bars[0].get("class"),
"primary_title": bars[0].xpath("./h1[@class='title']/text()")[0],
"button_groups": len(bars[0].xpath("./div[@class='buttons']")),
"has_subheader": "bar-subheader" in (bars[1].get("class") or ""),
}
expected_hdr = {
"bar_count": 2,
"primary_align": "left",
"primary_class": "bar-positive",
"primary_title": "Title!",
"button_groups": 2,
"has_subheader": True,
}
cases.append({
"name": "parse_ion_header_bar_and_subheader",
"summary": "解析 ion-header-bar 的 align-title、buttons 分组与 bar-subheader 副标题栏",
"expected": expected_hdr,
"actual": actual_hdr,
"passed": actual_hdr == expected_hdr,
})
# Case 2: Platform default title alignment & no-tap-scroll behavior
actual_api = {
"ios_default": resolve_title_align(None, "ios"),
"android_default": resolve_title_align(None, "android"),
"explicit_right": resolve_title_align("right", "android"),
"tap_scroll_enabled": simulate_header_tap(420, no_tap_scroll=False),
"tap_scroll_disabled": simulate_header_tap(420, no_tap_scroll=True),
}
expected_api = {
"ios_default": "center",
"android_default": "left",
"explicit_right": "right",
"tap_scroll_enabled": 0,
"tap_scroll_disabled": 420,
}
cases.append({
"name": "header_align_and_no_tap_scroll",
"summary": "验证 iOS/Android 默认标题对齐规则与 no-tap-scroll 禁止点顶回滚",
"expected": expected_api,
"actual": actual_api,
"passed": actual_api == expected_api,
})
# Case 3: Parse ion-footer-bar structure and single align-title API
ftr_markup = """
<div>
<ion-content>Some content!</ion-content>
<ion-footer-bar align-title="left" class="bar-assertive">
<div class="buttons"><button class="button">Left Button</button></div>
<h1 class="title">Title!</h1>
<div class="buttons" ng-click="doSomething()"><button class="button">Right Button</button></div>
</ion-footer-bar>
</div>
"""
fdoc = html.fromstring(ftr_markup)
ftr = fdoc.xpath("//ion-footer-bar")[0]
actual_ftr = {
"align_title": ftr.get("align-title"),
"class": ftr.get("class"),
"right_ng_click": ftr.xpath("./div[@class='buttons']")[1].get("ng-click"),
"title": ftr.xpath("./h1[@class='title']/text()")[0],
}
expected_ftr = {
"align_title": "left",
"class": "bar-assertive",
"right_ng_click": "doSomething()",
"title": "Title!",
}
cases.append({
"name": "parse_ion_footer_bar",
"summary": "解析 ion-footer-bar 的 align-title、bar-assertive 样式与左右按钮容器",
"expected": expected_ftr,
"actual": actual_ftr,
"passed": actual_ftr == expected_ftr,
})
# Case 4: Modern Ionic 7/8 <ion-header>/<ion-footer> + <ion-toolbar>
modern = html.fromstring(
'<div>'
'<ion-header><ion-toolbar color="primary">'
'<ion-buttons slot="start"><ion-button>Left</ion-button></ion-buttons>'
'<ion-title>Title!</ion-title>'
'</ion-toolbar></ion-header>'
'<ion-footer><ion-toolbar color="danger"><ion-title>Footer</ion-title></ion-toolbar></ion-footer>'
'</div>'
)
actual_modern = {
"header_toolbar_color": modern.xpath("//ion-header/ion-toolbar/@color")[0],
"buttons_slot": modern.xpath("//ion-buttons/@slot")[0],
"footer_toolbar_color": modern.xpath("//ion-footer/ion-toolbar/@color")[0],
}
expected_modern = {
"header_toolbar_color": "primary",
"buttons_slot": "start",
"footer_toolbar_color": "danger",
}
cases.append({
"name": "modern_header_footer_toolbar",
"summary": "核对现代 Ionic 7/8 <ion-header>/<ion-footer> 与 <ion-toolbar> 槽位映射",
"expected": expected_modern,
"actual": actual_modern,
"passed": actual_modern == expected_modern,
})
print(json.dumps({
"version": "CPython 3.12.14 / lxml offline Ionic JS directive & service verifier",
"cases": cases,
}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
未设置 align-title 时,iOS 默认居中对齐(center),Android 默认左对齐(left);将 no-tap-scroll 设为 true 可禁止点击顶部标题栏时内容自动滚回顶部。