如何在Javascript中获取iPad屏幕宽度

Mau*_*neo 9 javascript ipad screen-size

我需要使用Javascript从网页动态获取所有移动设备的屏幕大小.

我试过这个:

//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;
}
Run Code Online (Sandbox Code Playgroud)

但在iPad上我得到这个尺寸:980 x 1080(不是真正的768 x 1024).

谢谢

毛罗

Jac*_*sen 5

获取 iPad 上的屏幕尺寸需要读取屏幕对象的宽度和高度属性。

var height = screen.height;
var width = screen.width;
Run Code Online (Sandbox Code Playgroud)

根据方向,这些将提供 768 和 1024。

  • screen.width 和 screen.height 返回不正确的值。请看下面的观察结果: - 对于 iPad3,其返回 768 x 1024,而其原始分辨率为 1536 x 2048。 - 对于 iPhone5,其返回 320 x 568,其原始分辨率为 640 x 1136。根据我的观察, screen.width和 screen.height 对于 Android 设备来说工作得非常好。但对于 iOS 设备,它返回的值不正确。关于如何查找 iOS 设备的屏幕分辨率有什么想法吗? (2认同)