简体   繁体   English

隐藏地址栏时,移动浏览器不会启动调整大小事件

[英]Mobile browsers don't fire up resize event when hiding address bar

I have two sections on my website, whose height I set to window.innerHeight . 我的网站上有两个部分,其高度设置为window.innerHeight When the resize event is fired the height is updated. 触发resize事件时,将更新高度。

This works perfectly on my desktop, but on my android phone I get some problems. 这在我的桌面上完美运行,但在我的Android手机上我遇到了一些问题。

In Chrome for android, if you scroll down so that the address bar gets hidden and the window size changes, Chrome fires a resize event and the height is updated. 在Chrome for Android中,如果向下滚动以隐藏地址栏并更改窗口大小,Chrome会触发调整大小事件并更新高度。

Firefox and the default android browser don't fire this resize event after hiding the address bar. Firefox和默认的android浏览器在隐藏地址栏后不会触发此调整大小事件。 If I change the orientation of the device manually from portrait to landscape and back the height is updated like it should. 如果我手动将设备的方向从纵向更改为横向,则返回高度应该更新。

So I tried to dispatch an orientationchange event myself, but this didn't work. 所以我试着自己发送一个orientationchange事件,但这不起作用。

Is there a way I can get Firefox and the default browser to behave like Chrome? 有没有办法让Firefox和默认浏览器像Chrome一样?

Here is the website . 这是网站

Indeed mobile browsers (tested Android/Chrome iOs/Safari), when toggling address bar, fire the resize event only after user takes finger off the screen. 确实,移动浏览器(经过测试的Android / Chrome iOs / Safari)在切换地址栏时,只有在用户将手指从屏幕上移开后才会触发resize事件。

However we can use the touchmove event, that made the browser hide or show the address bar in the first place. 但是我们可以使用touchmove事件,这使得浏览器首先隐藏或显示地址栏。 In the handler we can use window.innerHeight property to detect changes in the viewport height. 在处理程序中,我们可以使用window.innerHeight属性来检测视口高度的变化。 Using jQuery: 使用jQuery:

$(window).on('resize touchmove', function () {
    // adjust heights basing on window.innerHeight
    // $(window).height() will not return correct value before resizing is done
});

It's not perfect, but nevertheless it is the only working solution I came up with. 它并不完美,但它是我想出的唯一可行的解​​决方案。

The whole behavior is kinda quirky, because even elements styled using position: fixed will be redrawn only after the touch ends. 整个行为有点古怪,因为即使使用position: fixed样式的元素也会在触摸结束后重绘。

Additionally if the adjustment function causes lag, the handler should probably be debounced. 此外,如果调整函数导致滞后,则处理程序应该被去抖动。

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

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