【问题标题】:Get correct window width after resize or orientationchange event for Android为 Android 调整大小或方向更改事件后获取正确的窗口宽度
【发布时间】:2012-05-10 09:25:20
【问题描述】:

我将函数绑定到 resize 和orientationchange 事件。在这个函数中,我试图读取窗口。 Iphone 总是给我正确的尺寸,但 Android 设备(2.2、2.3 和 4.0)似乎在实际更改 windowssize 之前触发了该事件。所以我总是得到最后一个窗口大小而不是最新的。有什么方法(没有超时)来获得正确的窗口大小?

我尝试了以下属性/功能: $(窗口).innerWidth(真) $(窗口).outerWidth(true) 屏幕宽度 $(window).width()

它们都没有给出正确的宽度。

有什么建议吗?

【问题讨论】:

  • 到目前为止,除了超时 500 毫秒之外,我没有看到其他解决方案......

标签: javascript jquery android


【解决方案1】:

幸运的是 window.orientation 在 onorientationchange 事件触发时已经更新。因此,作为一种解决方法,我将其打开:

switch(window.orientation) {
    case 90:
    case -90: // portrait;
        h = Math.max(window.innerWidth, window.innerHeight);
        w = Math.min(window.innerWidth, window.innerHeight);
        break;
    default: // landscape
        h = Math.min(window.innerWidth, window.innerHeight);
        w = Math.max(window.innerWidth, window.innerHeight);
        break;
}

不理想,但可以。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-31
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 2012-12-15
    • 2016-06-02
    • 1970-01-01
    相关资源
    最近更新 更多