Mobile Safari窗口报告980px
为什么在iPhone 4上报告980? 我以为它会是960或640。
alert($(window).width());
您正在获取默认视口设置。 请参阅Apple iOS文档 – 元标记并搜索“视口”。
楼盘简介
width视口的宽度(以像素为单位)。 默认值为980.范围为200到10,000。 您还可以将此属性设置为“number”中描述的常量。在iOS 1.0及更高版本中可用。
要获取设备宽度,请按照以下文档:
例如,要将视口宽度设置为设备的宽度,请将其添加到HTML文件中:
你试过以下吗?
alert($(document).width());
在safari中,默认窗口宽度为980px,因此如果您的javascript在加载窗口之前开始执行,则宽度将为980px,即使您的视口元标记将宽度指定为“设备宽度”。
如果您发现添加元标记无法解决您的问题,请确保您的Javascript代码仅在窗口加载后才执行。
在vanilla Javascript中:
console.log(window.innerWidth) // might be the default width window.onload= function () { console.log(window.innerWidth) // should now be device width //... your code here ... }
或者,如果使用jQuery:
console.log(window.innerWidth) // might be the default width $( document ).ready(function () { console.log(window.innerWidth) // should now be device width //... your code here ... })