【问题标题】:How to get iPad screen width in Javascript如何在 Javascript 中获取 iPad 屏幕宽度
【发布时间】:2012-06-02 09:38:18
【问题描述】:

我需要使用 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;
}

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

谢谢

毛罗

【问题讨论】:

标签: javascript ipad screen-size


【解决方案1】:

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

var height = screen.height;
var width = screen.width;

这些将根据方向提供 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 设备查找屏幕分辨率的任何想法?
  • @AnkitPrajapati 它返回 css 分辨率,这就是 screen.width 的用途。您应该始终将设备分辨率视为 css 分辨率。
  • @JoshLyness:感谢您的澄清。 :)
  • @AnkitPrajapati 我知道你现在可能已经意识到了,但以防万一其他人有同样的问题;)
猜你喜欢
  • 1970-01-01
  • 2013-10-11
  • 2017-10-01
  • 2011-09-25
  • 2011-08-06
  • 1970-01-01
  • 1970-01-01
  • 2020-01-03
  • 1970-01-01
相关资源
最近更新 更多