Considering that it is a behavior for mobile and iOS devices, this function maybe can help you. Validates the device, orientation and operating system.
import isMobile from 'ismobilejs'
/* For practical example: @see: https://www.npmjs.com/package/ismobilejs */
function windowSize() {
let [_width, _height] = [window.innerWidth, window.innerHeight]
// detect if device is mobile or tablet
if (isMobile(window.navigator).any) {
[_width, _height] = [window.screen.width, window.screen.height]
// detect if device is iOS and horizontal orientation
if (/iPad|iPhone|iPod/.test(navigator.userAgent)) {
// Horizontal orientation are equal to -90 or 90 degrees
if (Math.abs(window.orientation) == 90) {
_width = window.screen.height
_height = window.screen.width
}
}
}
return {
width: _width,
height: _height
}
}
Call this function when orientation change on mobile, or window resize on desktop.
let _windowSize = windowSize() // Return { width: realWidth, height: realHeight }
if (isMobile(window.navigator).any) {
window.addEventListener("resize", () => { _windowSize = windowSize() })
} else {
window.addEventListener("orientationchange", () => { _windowSize = windowSize() })
}