使用javascript查找屏幕尺寸,以英寸为单位而不是像素 [英] find screen dimensions in inches not pixels using javascript

查看:111
本文介绍了使用javascript查找屏幕尺寸,以英寸为单位而不是像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关SO的许多问题,这些问题涉及以像素为单位检测屏幕尺寸的最佳方法,而最终取决于分辨率来找到实际的屏幕尺寸.

I was reading many questions on SO regarding best ways to detect screen size in pixels which is ultimately dependant on resolution to find the actual screen size.

我知道某些移动设备的屏幕很小(例如4英寸),但分辨率很高.尽管某些设备具有大屏幕(例如7英寸标签),但分辨率却很低.

I know some mobile devices have small screen (say 4inch) yet the resolution is high. While some devices r having large screen (say 7inch tab) yet the resolution is low.

因此,当您想向用户显示页面时,您确实需要屏幕尺寸(以英寸或厘米为单位),以提供最佳的视觉舒适感.

So when you want to display your page to the user, you really need the screen size in inches or centimetres to give the best view comforting the eyes.

所以我问: 是否可以找到设备的屏幕尺寸(英寸)而不是像素? 只要我们知道解决方案,我就知道可以! !Bec我们可以根据屏幕的宽度和高度来计算尺寸

So I ask: Is there away to find out screen size of the device in inches not pixels ? I know it can be, provided that we know the resolution! !Bec we can calculate the size from screen width and height

欣赏任何想法!

推荐答案

一旦您获得了以像素为单位的屏幕分辨率,就可以使用隐藏的div计算出dpi了:

once you have got the screen resolution in pixels, you can work out the dpi with a hidden div:

<div id="dpi" style="height: 1in; width: 1in; left: 100%; position: fixed; top: 100%;"></div>

js

var dpi_x = document.getElementById('dpi').offsetWidth;
var dpi_y = document.getElementById('dpi').offsetHeight;

然后以英寸为单位计算分辨率:

then work out the resolution in inches:

var width = screen.width / dpi_x;
var height = screen.height / dpi_y;

示例

这篇关于使用javascript查找屏幕尺寸,以英寸为单位而不是像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆