Ban*_*ana 11 javascript mobile
我正在设计一个移动网站,其中有一个部分可以从中下载壁纸.为了容纳许多用户,我希望能够根据屏幕分辨率下载壁纸.我想从JavaScript检测分辨率并显示适当的壁纸.
这是我在网上找到并试过xD失败的内容:
width = window.innerWidth || document.body.clientWidth
height = window.innerHeight || document.body.clientHeight;
Run Code Online (Sandbox Code Playgroud)
对于我的SGS3,其分辨率为720x1280,我得到了360x567.
我该如何从JavaScript中发现手机的分辨率?
小智 31
您也许可以使用该screen对象:
var w = screen.width;
var h = screen.height;
Run Code Online (Sandbox Code Playgroud)
更新 - 尝试将其与以下内容一起使用window.devicePixelRatio:
var ratio = window.devicePixelRatio || 1;
var w = screen.width * ratio;
var h = screen.height * ratio;
Run Code Online (Sandbox Code Playgroud)
Ken Fyrstenberg,很棒的东西,谢谢。我一直在寻找一种简单的方法来检测所有可能的屏幕尺寸和分辨率数据,并回答这个比率有什么好处的问题,它让你看看它是否是视网膜显示类型的高分辨率设备。
wh:768x1024 cwh:768x905 rwh:1536x2048 ts:touch
Run Code Online (Sandbox Code Playgroud)
结合触摸测试检查这个相关的SO:
我可以很好地了解我们真实用户正在运行的设备的实际屏幕/触摸规格。
当然,对于网络内容,您真正感兴趣的是实际浏览器内部窗口的大小,即您在设备上分配的空间。顺便说一句,我已经看到苹果设备似乎总是提供纵向模式尺寸,例如,768 x 1024 用于非视网膜显示 iPad,这有点烦人,因为我还希望了解大多数用户的实际情况与网站和我们的网站互动。
Android 似乎给出了那个时刻的实际宽度/高度,即横向或纵向宽度/高度。
例如:
var ratio = window.devicePixelRatio || 1;
var is_touch_device = 'ontouchstart' in document.documentElement;
var touch_status = (is_touch_device) ? 'touch' : 'no-touch';
touch_status = ' ts:' + touch_status;
var width_height = 'wh:' + screen.width + 'x' + screen.height;
var client_width_height = ' cwh:' + document.documentElement.clientWidth + 'x' + document.documentElement.clientHeight;
var rw = screen.width * ratio;
var rh = screen.height * ratio;
var ratio_width_height = ' r:' + ratio + ' rwh:' + rw + 'x' + rh;
var data_string = width_height + client_width_height + ratio_width_height + touch_status;
Run Code Online (Sandbox Code Playgroud)
这会创建大量有关客户端系统的数据,然后您可以使用任何您喜欢的方法将这些数据传递给某些内容。
更新:使用这种方法只需要几分钟就可以找到苹果设备如何实际报告它们的宽度/高度:
wh:375x667 cwh:667x375 r:2 rwh:750x1334 ts:touch Device type: mobile
Run Code Online (Sandbox Code Playgroud)
如您所见,document.documentElement.clientWidth如果纵向/横向,该方法报告实际宽度,好东西。
| 归档时间: |
|
| 查看次数: |
36530 次 |
| 最近记录: |