javascript 如何获得屏幕的物理尺寸(即以英寸为单位)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11263747/
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
How to get screen's physical size (i.e. in inches)?
提问by Felix
I'm trying to figure out how to get the physical dimensions of a device's screen via Javascript. So far, my conclusion is that it's currently impossible, but I hope someone can prove me wrong :).
我试图弄清楚如何通过 Javascript 获取设备屏幕的物理尺寸。到目前为止,我的结论是目前这是不可能的,但我希望有人能证明我是错的:)。
So far I have tried to get this information by finding the device's DPI, but it seems there is no way to get the correct value here, as all devices I have tested (some HDPI & XHDPI Android devices, an iPhone4S, an iPad 2 and an iPad 3) report 96DPI.
到目前为止,我已尝试通过查找设备的 DPI 来获取此信息,但似乎无法在此处获得正确的值,因为我测试过的所有设备(一些 HDPI 和 XHDPI Android 设备、iPhone4S、iPad 2 和iPad 3) 报告 96DPI。
The first method of obtaining the DPI I tried is one you can find everywhere on the internet: create a div
with a CSS width of 1in
, get its clientWidth
or offsetWidth
and there's your DPI. Doesn't work, all devices report 96.
我尝试过的第一种获取 DPI 的方法是您可以在互联网上随处找到的方法:创建div
一个 CSS 宽度为 的1in
,获取它的clientWidth
or ,offsetWidth
然后就是您的 DPI。不起作用,所有设备都报告 96。
The second method was using the resolution media query, something along the lines of:
第二种方法是使用分辨率媒体查询,大致如下:
for (var i=90; i < 400; i++) {
if (matchMedia('(resolution: ' + i + 'dpi)').matches) {
return i;
}
}
I thought that was a smart solution, but unfortunately that returns 96 as well.
我认为这是一个聪明的解决方案,但不幸的是,它也返回了 96。
Is there anything left that I can try?
还有什么我可以尝试的吗?
采纳答案by rickster
96 "DPI" is a web standard that has little to do with reality. The inches it measures are best considered "logical" inches, which correspond to font metrics and CSS measurements (which can include points and inches). A "point" in typography is defined to be 1/72 inch, but screens stopped being consistently 72 DPI ages ago. Thus, all a CSS point really means now is that a 96 point font is 72 pixels tall. (And that's logical pixels, since the issue is now further conflated by hi-DPI screens.)
96“DPI”是一个与现实关系不大的网络标准。它测量的英寸最好被认为是“逻辑”英寸,它对应于字体度量和 CSS 度量(可以包括点和英寸)。排版中的“点”被定义为 1/72 英寸,但屏幕在很久以前就不再是 72 DPI。因此,CSS 点现在真正意味着 96 点字体是 72 像素高。(这是逻辑像素,因为这个问题现在被高 DPI 屏幕进一步混淆了。)
Anyhow, most native operating systems don't know a thing about their true physical screen size, so they don't even have information about such that they could expose to web apps via a browser. What you're asking isn't possible.
无论如何,大多数原生操作系统对它们真实的物理屏幕尺寸一无所知,因此它们甚至没有关于它们可以通过浏览器暴露给网络应用程序的信息。你问的是不可能的。