如何在Javascript中获取iPad屏幕宽度
我需要从使用Javascript的网页动态获取所有移动设备的屏幕大小。如何在Javascript中获取iPad屏幕宽度
我已经试过这样:
//get window's size
if (document.body && document.body.offsetWidth) {
windowsWidth = document.body.offsetWidth;
windowsHeight = document.body.offsetHeight;
}
if (document.compatMode=='CSS1Compat' &&
document.documentElement &&
document.documentElement.offsetWidth) {
windowsWidth = document.documentElement.offsetWidth;
windowsHeight = document.documentElement.offsetHeight;
}
if (window.innerWidth && window.innerHeight) {
windowsWidth = window.innerWidth;
windowsHeight = window.innerHeight;
}
不过在iPad上我得到这个尺寸:980×1080(不是真正的768×1024)。
由于
莫罗
获取iPad上屏幕尺寸需要阅读屏幕对象的宽度和高度属性。
var height = screen.height;
var width = screen.width;
这些将根据方向提供768和1024。
screen.width和screen.height返回的值不正确。 看看下面的意见: - 对于iPad3的其返回768×1024,其中它的原始分辨率为1536 X 2048。 - 对于iPhone5的其返回320×568,其中以原始分辨率为640×1136 按我的观察,screen.width和screen.height工作完美的Android设备。但对于iOS设备,它返回的值不正确。 有关如何查找iOS设备的屏幕分辨率的任何想法? –
@AnkitPrajapati它返回的CSS分辨率,这是screen.width的目的。您应该始终将设备分辨率视为css分辨率。 –
@JoshLyness:谢谢你的澄清。 :) –
这里,看到这个答案:http://stackoverflow.com/q/6850164/1291428 – Sebas
窗口大小**!= **屏幕的分辨率。 – gdoron
你尝试过屏幕对象吗? – Zaffy