Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

javascript - What are best practices for detecting pixel ratio/density?

I am currently using JavaScript for mobile device detection on my website, this then allows me to serve different content for mobile or desktop users.

Currently I use window.devicePixelRatio and screen.width to work out if the user if on a mobile device or not, like so:

var isMobileScreenWidth = ((screen.width / window.devicePixelRatio) < 768)

768px is the point at which we define mobile or desktop so 767 and below is mobile and 768 and above is desktop.

This works perfectly, but I have recently come across an issue with Firefox, when Firefox is zoomed in and out it changes the window.devicePixelRatio, so:

zoom = 30%, window.devicePixelRatio = 0.3
zoom = 100%, window.devicePixelRatio = 1.0
zoom = 300%, window.devicePixelRatio = 3.0

This now causes me a problem because any users which have their browser zoomed in on Firefox get the mobile version of the site.

I was wondering if anyone knew of a different or better way of getting the pixel density which is separate from desktop browsers.

I do use a small amount of User Agent detection as well but because it is a massive job to keep up with the constantly changing list of mobile user agents it is not possible for me to depend on both the screen resolution and user agent at the same time.

If anyone has any ideas about this and can help that would be awesome.

UPDATE:

I have just come across this:

window.screen.availWidth / document.documentElement.clientWidth

This quick bit of math is suggested in this post:

window.devicePixelRatio does not work in IE 10 Mobile?

I have tested it and it work in Firefox, and solves my problem, but, unfortunately now causes a problem in Chrome, so:

Chrome zoom = 100%,
window.devicePixelRatio = 1.0,
window.screen.availWidth / document.documentElement.clientWidth = 3.0

I cannot seem to find a solid solution which works for everything.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should leverage the manufacturer's hint via the <meta name="viewport" content="width=device-width"/> or @-ms-viewport {width:device-width} feature. It basically exposes the default zoom the device manufacturer considers optimal given the pixel density of the screen. After you do that, when you call window.innerWidth it will give you what your original equation was intended for but without relying on a measurement of pixel density.

Avoid relying on window.devicePixelRatio for anything. Its meaning and the value it returns is currently in a state of flux and anything you make right now based around it will most likely break very soon.

Note: Meta viewport only works on Android, iOS, and Windows Phone 8. @-ms-viewport only works (properly) on IE10 Metro and can interfere with proper Meta viewport behavior on Windows Phone 8.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...