当前位置: 代码迷 >> JavaScript >> 为什么在应用程序启动时android webview在HTML5上获得错误的宽度
  详细解决方案

为什么在应用程序启动时android webview在HTML5上获得错误的宽度

热度:30   发布时间:2023-06-05 09:39:51.0

使用Android模拟器genymotion(800 * 1280) ,获取window.innerWidth是错误的,描述如下:

安装android应用后,我在事件window.onload = function(){alert(window.innerWidth)}中设置js语言来获取视口宽度,但答案是320(这是错误的),然后单击一个按钮,触发事件$(button).click(function(){alert(window.innerWidth)}),但答案是602(在右边)。

当然, <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">已附加到HTML5头。 尝试使用Chrome移动调试器(800 * 1280)时没有问题。答案是800。

<button value="click me get the width"/>
<script>
 //we all know window.innerWidth can get the viewport width
window.onload=functon(){
    alert(window.innerWidth)
}//get the wrong answer 320
$(button).click(function(){
    alert(window.inerWidth)})//get the right answer 602
</script>

试试这个吧。

我的用例是按自定义分辨率渲染网站,而不一定是设备的分辨率。

我无法得到与所期望的结果WebView任何责任。 AdvancedWebView首次尝试工作。

  相关解决方案