jQuery Mobile 方向改变事件

jQuery Mobile 的「屏幕方向改变事件」是最容易被忽略、但在移动端又超级实用的一个事件,尤其在做仪表盘、大表格、图表、游戏、拍照类 App 时,几乎 100% 会用到。

一、jQuery Mobile 官方原生支持的事件(推荐!)

// 最简单、最稳、最通用(2010–2025 年都适用)
$(window).on("orientationchange", function(event) {
    // event.orientation 的值:
    // "portrait"  → 竖屏(包括正竖、倒竖)
    // "landscape" → 横屏(包括左横、右横)

    if (event.orientation === "landscape") {
        console.log("现在是横屏");
        // 做横屏专属布局
    } else {
        console.log("现在是竖屏");
        // 恢复竖屏布局
    }
});

这就是 jQuery Mobile 给你封装好的最完美事件,零延迟、零判断错误、所有 Android/iOS 老机器都完美支持。

二、2025 年最推荐的完整写法(直接复制进项目)

$(window).on("orientationchange", function(event) {
    var ori = event.orientation;   // "portrait" 或 "landscape"
    var $page = $.mobile.activePage;  // 当前页面(jQM 专用)

    // 1. 给 body 加 class,方便 CSS 控制
    $("body")
        .removeClass("orientation-portrait orientation-landscape")
        .addClass("orientation-" + ori);

    // 2. 强制刷新页面布局(关键!防止控件错位)
    if ($page) {
        // 重新计算所有控件尺寸
        $page.trigger("updatelayout");

        // 如果有 listview、grid、table,必须 refresh
        $page.find("ul[data-role='listview']").listview("refresh");
        $page.find(".ui-grid-a, .ui-grid-b, .ui-grid-c").trigger("updatelayout");
    }

    // 3. 延迟一点点再执行重排(解决部分安卓机延迟渲染问题)
    setTimeout(function() {
        $(window).trigger("resize");   // 触发一次 resize 也能解决很多问题
    }, 200);
});

配合 CSS 一发入魂:

/* 竖屏样式 */
.orientation-portrait .hide-in-portrait  { display: none !important; }
.orientation-portrait .big-chart        { width: 100%; height: 300px; }

/* 横屏样式 */
.orientation-landscape .hide-in-landscape { display: none !important; }
.orientation-landscape .big-chart        { width: 100%; height: 500px; }
.orientation-landscape .ui-grid-a        { font-size: 14px; }

三、经典实战案例(直接可用的 5 个场景)

  1. 横屏时隐藏底部导航栏
$(window).on("orientationchange", function(e) {
    if (e.orientation === "landscape") {
        $("div[data-role='footer']").hide();
    } else {
        $("div[data-role='footer']").show();
    }
});
  1. 横屏时强制显示大表格
<table id="dataTable" class="small-in-portrait">
    <!-- 很多列 -->
</table>
$(window).on("orientationchange", function(e) {
    $("#dataTable")
        .toggleClass("small-in-portrait", e.orientation === "portrait")
        .trigger("updatelayout");
});
  1. 拍照/扫描页面横屏提醒
$(window).on("orientationchange", function(e) {
    if (e.orientation === "portrait") {
        alert("请横屏使用,效果更佳!");
    }
});

四、补充:如果你非要精确判断四个方向(极少需要)

window.addEventListener("orientationchange", function() {
    var o = window.orientation;   // 原生值
    // 0   → 正竖屏
    // 90  → 右横屏
    // -90 → 左横屏
    // 180 → 倒竖屏(部分安卓支持)

    switch(window.orientation) {
        case 0:    console.log("正竖屏"); break;
        case 90:   console.log("右横屏"); break;
        case -90:  console.log("左横屏"); break;
        case 180:  console.log("倒竖屏"); break;
    }
});

但 99% 的项目用 event.orientation 的 “portrait/landscape” 就完全够了,而且最稳定。

总结(2025 年还在用 jQM 的你一定要记住)

$(window).on("orientationchange", function(e) {
    $("body").toggleClass("landscape", e.orientation === "landscape");
    $.mobile.activePage && $.mobile.activePage.trigger("updatelayout");
});

这三行代码,解决你 95% 的横竖屏适配问题,10 年不坏!

需要我给你打包一个「jQuery Mobile 横竖屏完美适配 Demo」(包含 8 个常见场景:表格、图表、视频、导航隐藏、网格布局自动切换等),直接下载就能跑,告诉我一声就发你!

文章已创建 2588

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部