在移动网站上获取屏幕分辨率 [英] Get screen resolution on a mobile website

查看:117
本文介绍了在移动网站上获取屏幕分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个移动网站,其中有一个部分可以从中下载壁纸。为了容纳许多用户,我希望能够根据屏幕分辨率下载壁纸。我想从JavaScript中检测分辨率并显示相应的壁纸。

I am designing a mobile website that has a section to download wallpapers from. In order to accommodate many users, I wish to make it possible to download a wallpaper based on the screen resolution. I want to detect resolution from JavaScript and show the appropriate wallpaper.

这是我在网上找到并尝试失败的xD:

This is what I found online and tried and failed xD:

width  = window.innerWidth  || document.body.clientWidth
height = window.innerHeight || document.body.clientHeight;

对于我的SGS3,其分辨率为720x1280,我得到360x567。

For my SGS3, which has the resolution 720x1280 I get the 360x567.

如何从JavaScript中发现手机的分辨率?

How should I discover the resolution of the phone from JavaScript?

推荐答案

您也许可以使用 screen object:

You can perhaps use the screen object:

var w = screen.width;
var h = screen.height;

更新 - 尝试将其与一起使用window.devicePixelRatio

Update - Try to use it with the window.devicePixelRatio:

var ratio = window.devicePixelRatio || 1;
var w = screen.width * ratio;
var h = screen.height * ratio;

这篇关于在移动网站上获取屏幕分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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