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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10777547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:00:59  来源:igfitidea点击:

How to get iPad screen width in Javascript

javascriptipadscreen-size

提问by Mauro Cattaneo

I need to dynamically get the screen size of all mobile devices from a webpage using Javascript.

我需要使用 Javascript 从网页动态获取所有移动设备的屏幕尺寸。

I have tried this:

我试过这个:

//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;
}

But on iPad I get this size: 980 x 1080 (not the real 768 x 1024).

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

Thanks

谢谢

Mauro

毛罗

回答by Jacob T. Nielsen

Getting the screen dimensions on iPad requires reading the width and height properties of the screen object.

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

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

These will provide 768 and 1024 depending on orientation.

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