简体   繁体   English

iOS Chrome 产生不正确的 window.innerWidth 和 innerHeight

[英]iOS Chrome yields incorrect window.innerWidth and innerHeight

Google Chrome version 104 on iOS reports incorrect window.innerWidth and window.innerHeight values after I rotate my device.旋转设备后,iOS 上的 Google Chrome 版本 104 报告不正确window.innerWidthwindow.innerHeight值。 For example:例如:

  • I load my page on portrait mode, and I get 414 x 714 .我以纵向模式加载页面,得到414 x 714
  • I rotate my phone to landscape, then back to portrait mode, and I get 390 x 334我将手机旋转到横向,然后回到纵向模式,得到390 x 334

I've created a snippet below to test.我在下面创建了一个片段来测试。 It might take 3 attempts before the wrong values start showing up:在错误值开始出现之前可能需要 3 次尝试:

 const $ = document.getElementById.bind(document); const w1 = $("w1"); const h1 = $("h1"); const w2 = $("w2"); const h2 = $("h2"); // Get width/height on page load w1.innerText = `w: ${window.innerWidth}`; h1.innerText = `h: ${window.innerHeight}`; // Get width/height on resize event function onResizeInstant() { w2.innerText = `w: ${window.innerWidth}`; h2.innerText = `h: ${window.innerHeight}`; } window.addEventListener("resize", onResizeInstant);
 body{font-family: sans-serif} h1{font-size: 16px} div{ font-size: 16px; height: 20px; background: #eee; margin-bottom: 2px; }
 <h1>window.innerWidth + Height on initial page load</h1> <div id="w1"></div> <div id="h1"></div> <h1>window.innerWidth + Height after rotating device</h1> <div id="w2"></div> <div id="h2"></div>

This does not happen on iOS Safari, Chrome Android, Firefox, or any desktop device.这不会发生在 iOS Safari、Chrome Android、Z763F7F1AEC350CD1A46238D1D5Z3C 或任何桌面设备上。 It only happens in Google Chrome for iOS.它只发生在 iOS 的谷歌浏览器中。 Does anybody know the source of this behavior, or a workaround to this bug?有人知道这种行为的根源,或者这个错误的解决方法吗?

This is definitely a bug only with Google Chrome in iOS.这绝对是 iOS 中的谷歌浏览器的错误。 The solution is to add a short setTimeout() before reading the window.innerWidth and window.innerHeight values.解决方案是在读取window.innerWidthwindow.innerHeight值之前添加一个短的 setTimeout() 。

 const $ = document.getElementById.bind(document); const w1 = $("w1"); const h1 = $("h1"); const w2 = $("w2"); const h2 = $("h2"); // Called instantly on resize event // Could yield incorrect values on Google Chrome on iOS at random function onResizeInstant() { w1.innerText = `w: ${window.innerWidth}`; h1.innerText = `h: ${window.innerHeight}`; window.setTimeout(onResizeTimeout, 5); } // Called after 5ms timeout // Will yield accurate values function onResizeTimeout() { w2.innerText = `w: ${window.innerWidth}`; h2.innerText = `h: ${window.innerHeight}`; } window.addEventListener("resize", onResizeInstant); // Call on load onResizeInstant();
 body{font-family: sans-serif} h1{font-size: 16px} div{ font-size: 16px; height: 20px; background: #eee; margin-bottom: 2px; }
 <h1>window.innerWidth + Height before setTimeout()</h1> <div id="w1"></div> <div id="h1"></div> <h1>window.innerWidth + Height after setTimeout()</h1> <div id="w2"></div> <div id="h2"></div>

Results:结果:

在此处输入图像描述

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

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