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

jQuery Mobile 方向改变事件

讲解 jQuery Mobile 绑定在 window 上的 orientationchange 事件、回调函数 event.orientation 的 portrait/landscape 返回值、结合 window.orientation 角度区分横竖屏样式的做法,以及现代 Screen Orientation API 与 CSS @media (orientation) 迁移方案。

学完这一章,你能够

  • 讲解 jQuery Mobile 绑定在 window 上的 orientationchange 事件、回调函数 event.orientation 的 portrait/landscape 返回值、结合 window.orientation 角度区分横竖屏样式的做法,以及现代 Screen Orientation API 与 CSS @media (orientation) 迁移方案。

orientationchange 事件绑定与 event.orientation

当移动设备在竖屏(纵向)与横屏(横向)之间旋转时,jQuery Mobile 会在 `window` 对象上触发 `orientationchange` 事件,基础绑定写法为 `$(window).on("orientationchange", function(){ ... })`。回调函数的 `event` 参数包含 `event.orientation` 属性,用于返回设备当前方向:纵向(垂直握持)对应 `"portrait"`,横向(水平握持)对应 `"landscape"`。(来源页面上方曾包含一段通过两个按钮旋转示例手机图片 `iphone5.png` 的内联演示脚本,已按安全规则仅静态登记,未联网抓图或执行来源脚本。)

window.orientation 角度分支与现代 API 迁移

由于 `orientationchange` 绑定在 `window` 上,早期移动端代码常进一步读取 `window.orientation` 的旋转角度来动态切换样式:纵向视图返回 `0`(倒置纵向为 `180`),横向视图返回 `90` 或 `-90`。例如当 `window.orientation == 0` 时将段落设为黄底与 `300%` 字号,否则设为粉底与 `200%` 字号。需要着重指出:随着 jQuery Mobile 归档,`window.orientation` 在现代 Web 标准中也已被正式废弃;当代项目应使用 CSS `@media (orientation: portrait)` / `@media (orientation: landscape)` 控制横竖屏样式,或在脚本中使用 W3C Screen Orientation API(`screen.orientation.type`、`screen.orientation.angle` 与 `change` 事件)及 `window.matchMedia("(orientation: portrait)")`。

window.orientation 返回角度值说明

主题角度值与现代替代
纵横向角度 0 / 90 / -90 与现代迁移旧版 window.orientation 在纵向视图返回 0、横向视图返回 90 或 -90;该属性在现代标准中已废弃,应迁移至 screen.orientation 与 CSS @media (orientation)。

运行 jQuery Mobile orientationchange 方向判断与样式分支离线验证

含完整源码 · 115 行
展开查看收起
python
"""Offline jQuery Mobile Orientationchange event & style verifier (Chapter 26)."""
from __future__ import annotations

import json
from lxml import html


def angle_to_orientation(angle: int) -> str:
    return "portrait" if angle in (0, 180) else "landscape"


def style_for_orientation(angle: int) -> dict[str, str]:
    if angle == 0:
        return {"background-color": "yellow", "font-size": "300%"}
    return {"background-color": "pink", "font-size": "200%"}


def main() -> None:
    cases = []

    # Case 1: window_orientationchange_binding
    transitions = [0, 90, 0, -90]
    fired = []
    prev = None
    for ang in transitions:
        ori = angle_to_orientation(ang)
        if prev is not None and ori != prev:
            fired.append((prev, ori))
        prev = ori
    actual1 = {"binding_target": "window", "change_count": len(fired), "transitions": fired}
    expected1 = {
        "binding_target": "window",
        "change_count": 3,
        "transitions": [("portrait", "landscape"), ("landscape", "portrait"), ("portrait", "landscape")],
    }
    cases.append({
        "name": "window_orientationchange_binding",
        "summary": "验证绑定在 window 上的 orientationchange 在纵横屏交替旋转时准确触发。",
        "expected": expected1,
        "actual": actual1,
        "passed": actual1 == expected1,
    })

    # Case 2: event_orientation_values
    actual2 = {str(a): angle_to_orientation(a) for a in (0, 90, -90, 180)}
    expected2 = {"0": "portrait", "90": "landscape", "-90": "landscape", "180": "portrait"}
    cases.append({
        "name": "event_orientation_values",
        "summary": "验证 event.orientation 将 0/180 度映射为 portrait、90/-90 度映射为 landscape。",
        "expected": expected2,
        "actual": actual2,
        "passed": actual2 == expected2,
    })

    # Case 3: orientation_style_branching
    doc = html.fragment_fromstring('<div><p id="target">旋转设备查看样式</p></div>', create_parent=False)
    actual3 = {
        "has_target_p": bool(doc.xpath(".//p[@id='target']")),
        "angle_0": style_for_orientation(0),
        "angle_90": style_for_orientation(90),
        "angle_neg90": style_for_orientation(-90),
    }
    expected3 = {
        "has_target_p": True,
        "angle_0": {"background-color": "yellow", "font-size": "300%"},
        "angle_90": {"background-color": "pink", "font-size": "200%"},
        "angle_neg90": {"background-color": "pink", "font-size": "200%"},
    }
    cases.append({
        "name": "orientation_style_branching",
        "summary": "验证按 window.orientation == 0 与 90/-90 为段落分别切换纵向黄底与横向粉底样式。",
        "expected": expected3,
        "actual": actual3,
        "passed": actual3 == expected3,
    })

    # Case 4: modern_screen_orientation_api
    modern_map = {
        0: "portrait-primary",
        90: "landscape-primary",
        -90: "landscape-secondary",
        180: "portrait-secondary",
    }
    actual4 = {
        "window_orientation_deprecated": True,
        "screen_orientation_types": modern_map,
        "css_media_queries": ["@media (orientation: portrait)", "@media (orientation: landscape)"],
    }
    expected4 = {
        "window_orientation_deprecated": True,
        "screen_orientation_types": {
            0: "portrait-primary",
            90: "landscape-primary",
            -90: "landscape-secondary",
            180: "portrait-secondary",
        },
        "css_media_queries": ["@media (orientation: portrait)", "@media (orientation: landscape)"],
    }
    cases.append({
        "name": "modern_screen_orientation_api",
        "summary": "核对已废弃 window.orientation 到 W3C Screen Orientation API 与 CSS orientation 媒体查询的迁移对照。",
        "expected": expected4,
        "actual": actual4,
        "passed": actual4 == expected4,
    })

    print(json.dumps({
        "version": "CPython 3.12.14 / lxml offline jQuery Mobile DOM & reference verifier",
        "cases": cases,
    }, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
验证 window 绑定触发计数、0/180/90/-90 角度到 portrait/landscape 映射、纵横屏样式分支与 Screen Orientation API 对照。

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

本地运行

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

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

本章附件与实例包

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

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

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

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

常见错误与边界检查

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

技术规范与官方资料

练习与参考解答

查看参考解答

window.orientation 已从现代 Web 标准废弃且跨设备角度语义不一致;样式切换应优先使用 CSS @media (orientation: portrait|landscape),脚本监听应改用 screen.orientation(type/angle/change)或 window.matchMedia。

SITE SEARCH

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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