检测像素比/密度的最佳做法是什么? [英] What are best practices for detecting pixel ratio/density?

查看:198
本文介绍了检测像素比/密度的最佳做法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在我的网站上使用JavaScript进行移动设备检测,这样我就可以为移动或桌面用户提供不同的内容。

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.

目前我使用 window.devicePixelRatio screen.width 计算用户是否在移动设备上,如下:

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 是我们定义移动或桌面的点,因此767及以下是移动和768以上是桌面。

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

这很有效,但我最近遇到了Firefox的问题,当Firefox放大和缩小时它会改变 window.devicePixelRatio ,所以:

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

现在这给我带来了麻烦任何浏览器放大Firefox浏览器的用户都可以获得该网站的移动版本。

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.

更新:

我刚刚遇到过这个问题:

I have just come across this:

window.screen.availWidth / document.documentElement.clientWidth

这篇文章建议使用这一快速的数学运算:

This quick bit of math is suggested in this post:

window.devicePixel比率在IE 10移动版中不起作用?

我测试了它并且它在Firefox中运行,并解决了我的问题,但不幸的是现在导致了问题在Chrome中,所以:

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.

推荐答案

您应该通过< meta name =viewport利用制造商的提示content =width = device-width/> @ - ms-viewport {width:device-width} 功能。它基本上暴露了设备制造商认为最佳的默认缩放,给定屏幕的像素密度。执行此操作后,当您调用 window.innerWidth 时,它将为您提供原始等式的用途,但不依赖于像素密度的测量。

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.

避免依赖 window.devicePixelRatio 。它的意义和它返回的价值目前处于不稳定状态,你现在所做的任何事情很可能很快就会破裂。

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.

注意:
Meta视口仅适用于Android,iOS和Windows Phone 8. @ - ms-viewport 仅在IE10 Metro上正常工作,并且可能干扰正确的Meta视口行为Windows Phone 8。

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.

这篇关于检测像素比/密度的最佳做法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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