简体   繁体   English

Android调整大小或orientationchange事件后获取正确的窗口宽度

[英]Get correct window width after resize or orientationchange event for Android

I bound functions to the resize and orientationchange event. 我将函数绑定到resize和orientationchange事件。 In this function i'm trying to read the windowwith. 在这个函数中我试图读取windowwith。 Iphone always gives me the correct sizes but Android devices (2.2, 2.3 and 4.0) seem to trigger the event before actually changing the windowssize. Iphone总是给我正确的尺寸,但Android设备(2.2,2.3和4.0)似乎在实际更改windowssize之前触发事件。 So I always get the last windowsize and not the newest. 所以我总是得到最后的windowsize而不是最新的。 Is there some way (without timeouts) to get the correct windowsize? 是否有某种方式(没有超时)来获得正确的窗口大小?

I tried following attributes / functions: $(window).innerWidth(true) $(window).outerWidth(true) screen.width $(window).width() 我尝试了以下属性/函数:$(window).innerWidth(true)$(window).outerWidth(true)screen.width $(window).width()

None of them are giving the correct width. 他们都没有给出正确的宽度。

Any suggestions? 有什么建议?

Fortunately window.orientation is already updated when the onorientationchange event fires. 幸运的是,当onorientation事件触发时,window.orientation已经更新。 So as a workaround I switch on that: 因此,作为一种解决方法,我打开它:

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;
}

Not ideal, but it works. 不理想,但它的工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM