Get the correct window width after resizing or changechange event for Android

I bound functions to a resize and orientation event. In this function, I am trying to read a window. Iphone always gives me the correct sizes, but Android devices (2.2, 2.3 and 4.0) seem to fire an event before the window actually changes. Therefore, I always get the latest windowsize, not the newest. Is there any way (without timeouts) to get the correct window?

I tried the following attributes / functions: $ (window) .innerWidth (true) $ (window) .outerWidth (true) screen.width $ (window) .width ()

None of them give the correct width.

Any suggestions?

+4
source share
1 answer

Fortunately, window.orientation is already updated when the onorientationchange event is fired. Therefore, as a workaround, I include the following:

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 perfect, but it works.

+3
source

Source: https://habr.com/ru/post/1411731/


All Articles