简体   繁体   中英

Get correct window width after resize or orientationchange event for Android

I bound functions to the resize and orientationchange event. In this function i'm trying to read the 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. So I always get the last windowsize and not the newest. 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()

None of them are giving the correct width.

Any suggestions?

Fortunately window.orientation is already updated when the onorientationchange event fires. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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