使用javascript查找屏幕尺寸,以英寸为单位而不是像素

编程入门 行业动态 更新时间:2024-10-27 17:14:16
本文介绍了使用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查找屏幕尺寸,以英寸为单位而不是像素

本文发布于:2023-07-19 09:01:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1154956.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:像素   而不是   单位   屏幕尺寸   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!