Android onOrientationChange 在方向完成之前触发(太早)

发布于 2024-12-18 14:49:06 字数 554 浏览 3 评论 0原文

每当用户旋转手机时,我需要读取 DIV 的像素宽度。在 iOS 上,以下代码将在方向完成后打印出 DIV 的宽度。但是,在 Android 上,代码将在方向开始之前打印出 DIV 的宽度。

HTML

<body>
    <div id="foo" style="width: 80%">foo</div>
</body>

JS

window.onorientationchange = function() {
    alert($('foo').getWidth());
}

例如,用户以纵向模式启动。他切换到横向模式。在 Android 上,代码将打印出较旧的较窄宽度,而 iOS 将打印出新的较宽宽度。

如何让 Android 的行为像 iOS 一样?在我的网络应用程序中,之后宽度对我来说很重要。 之前宽度对我来说毫无用处。

I need to read the pixel width of a DIV whenever the user rotates his phone. On iOS the following code will print out the DIV's width after the orientation completes. However, on Android the code will print out the DIV's width before the orientation starts.

HTML

<body>
    <div id="foo" style="width: 80%">foo</div>
</body>

JS

window.onorientationchange = function() {
    alert($('foo').getWidth());
}

For example, the user starts in portrait mode. He rotates to landscape mode. On Android, the code will print out the older narrower width, while iOS will print out the new wider width.

How do I make Android behave like iOS? The after width is important to me in my web application. The before width is useless to me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

几度春秋 2024-12-25 14:49:06

一个快速但肮脏的解决方案是使用计时器。您可以继续开发,直到出现更合适的解决方案。

编辑:刚刚认识到在 android 上 onorientationchange 事件在调整大小事件之前触发,这与 ipad 的行为不同。检查 Android 手机上的调整大小事件,这是否提供了正确的宽度?

a quick and dirty solution would be to use a timer. you can continue development until a more suitable solution comes up.

edit: just recognized that on android the onorientationchange event is fired before resize event, this is diffrent from ipads behaviour. check the resize event on android phones, does this provide the right width?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文