简体   繁体   English

如何使用 JavaScript 检测屏幕 DPI

[英]How to detect the screen DPI using JavaScript

The only way I found till now, is getting the offsetWidth, offsetHeight of a test div element with height and width of one inch: http://www.infobyip.com/detectmonitordpi.php到目前为止,我发现的唯一方法是获取heightwidth为一英寸的测试div元素的 offsetWidth、offsetHeight: http : //www.infobyip.com/detectmonitordpi.php

Is there a way to detect the screen DPI via JavaScript instead?有没有办法通过 JavaScript 检测屏幕 DPI?

Thanks,谢谢,

Atara.阿塔拉。

In webkit you can detect if your user has a so called "high dpi screen" by simply retrieving the value from:在 webkit 中,您可以通过简单地从以下位置检索值来检测您的用户是否有所谓的“高 dpi 屏幕”:

window.devicePixelRatio

Normal dpi screens will return 1. The iPhone 4 will return 2, but numbers like 1.8 or 2.12 are also possible.正常的 dpi 屏幕将返回 1。iPhone 4 将返回 2,但也可以使用 1.8 或 2.12 等数字。

<div id='testdiv' style='height: 1in; left: -100%; position: absolute; top: -100%; width: 1in;'></div>
<script type='text/javascript'>
    dpi_x = document.getElementById('testdiv').offsetWidth;
    dpi_y = document.getElementById('testdiv').offsetHeight;
</script>

Then you can use JQuery to send dpi_x and dpi_y this to to your server然后您可以使用 JQuery 将dpi_xdpi_y发送到您的服务器

http://jsfiddle.net/sxfv3/ http://jsfiddle.net/sxfv3/

There is, so far, no standard and supported-everywhere solution.到目前为止,还没有标准的、支持无处不在的解决方案。

window.devicePixelRatio , as suggested by laurens peeters, does work if you don't care about IE, or any browser from the ancient times, like early January 2013 (eg, Firefox 17). window.devicePixelRatio ,正如 laurens peeters 所建议的,如果你不关心 IE 或任何古代浏览器,比如 2013 年 1 月上旬(例如,Firefox 17),它确实有效。

See Cross Browser Retina/High Resolution Media Queries (and various comments and links there) for how to get this information as of late 2012, but you'll have to keep searching again, and adjusting your code, every so often until something finally gets standardized, and implemented in every browser, with widespread-enough version adoption that you can stop caring about older versions…请参阅Cross Browser Retina/High Resolution Media Queries (以及那里的各种评论和链接)以了解截至 2012 年底如何获取此信息,但您必须经常再次搜索并调整代码,直到最终得到某些信息标准化,并在每个浏览器中实现,具有足够广泛的版本采用,您可以不再关心旧版本......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM