简体   繁体   English

通过屏幕分辨率检测移动设备的可靠性如何?

[英]How reliable is detecting mobile devices by screen resolution?

This sounds a bit too good to be true, so please tell me if it is. 这听起来有点太好了,所以请告诉我它是不是真的。

If I have just one single version of a mobile website (no variations for different devices, just one website for all mobiles), how reliable it is to detect mobile devices by screen resolution? 如果我只有一个版本的移动网站(不同设备没有变化,只有一个网站适用于所有手机),通过屏幕分辨率检测移动设备的可靠性如何?

And simply serve the mobile version if screen resolution is < than say 400px. 如果屏幕分辨率小于400px,则只需提供移动版本。

NOTE: My question assumes that javascript is enabled. 注意:我的问题假设启用了javascript。 Also,I'm aware there's user agent detection, but I'd like to do without it. 此外,我知道有用户代理检测,但我想没有它。

Javascript mobile device screen detection for height is not reliable at all. Javascript移动设备屏幕检测高度根本不可靠。 The problem is that different browsers use different amounts of 'chrome' and different OS versions use different heights for the system bar. 问题是不同的浏览器使用不同数量的“chrome”,不同的OS版本对系统栏使用不同的高度。 All the detection mechanism report unreliably for height (screen.height, window.outerHeight, window.innerHeight - etc,etc) 所有的检测机制都报告不可靠的高度(screen.height,window.outerHeight,window.innerHeight - etc等)

Width seems to be most reliable on window.outerWidth across all OS's. 在所有操作系统的window.outerWidth上,宽度似乎最可靠。

Read a most excellent analytical report here: 在这里阅读最优秀的分析报告:

http://www.tripleodeon.com/2011/12/first-understand-your-screen/ http://www.tripleodeon.com/2011/12/first-understand-your-screen/

You will want to look into serving different stylesheets via media queries. 您将需要考虑通过媒体查询提供不同的样式表。 You can use queries to identify screen widths and only serve certain css to certain devices. 您可以使用查询来识别屏幕宽度,并仅为某些设备提供某些css。 For example this query would serve a iphone.css only to devices identified as having the typical dimensions of an iphone: 例如,此查询仅为识别为具有iphone典型尺寸的设备提供iphone.css:

<link media="only screen and (max-device-width: 480px)" href="iphone.css" type="text/css" rel="stylesheet" />

There's a detailed article on this subject over at alistapart alistapart上有关于这个主题的详细文章

Bear in mind though that not all devices recognize media queries. 请记住,并非所有设备都能识别媒体查询。 If you need to support lots of older devices like blackberry's and flip phones you should take the advise above for using UA detection - I know it feels wrong if you're coming from the desktop development world but really we have to use the tools we have available to us and Mobile Web is a growing but in many ways still a new horizon. 如果您需要支持许多旧设备,如黑莓手机和翻盖手机,您应该采用上面的建议来使用UA检测 - 我知道如果您来自桌面开发世界会感觉不对,但实际上我们必须使用我们拥有的工具对我们而言,移动网络正在发展,但在许多方面仍然是一个新的视野。

I came here because I had the same idea and question, and similar situation - the website already requires JavaScript and I'm doing a one-size-fits-all mobile web app, at least for now. 我来到这里是因为我有同样的想法和问题,以及类似的情况 - 网站已经需要JavaScript,我正在做一个适合所有人的移动网络应用程序,至少目前如此。 Our release cycle is really long - any UA detection I hard-code will be somewhat obsolete by the time the code is tested and released. 我们的发布周期非常长 - 在测试和发布代码时,任何UA检测我的硬编码都会有些过时。 Since the purpose of this alternate interface is to make it work on smaller screens, it would seem to make sense to use that test. 由于此备用接口的目的是使其在较小的屏幕上工作,因此使用该测试似乎是有意义的。 I don't know however, what size I would pick - I have a hunch mobile devices are not bound (even by convention) to particular screen dimensions. 然而,我不知道,我会选择多大的尺寸 - 我有一个预感移动设备不受特定屏幕尺寸的限制(即使按惯例)。 I guess we just have to decide at what point the main web page is no longer functional. 我想我们只需要确定主网页在什么时候不再起作用。

I can understand other people's hesitation to this approach because sometimes there are other issues with a standard site on a mobile device than just the screen size. 我可以理解其他人对此方法的犹豫,因为有时移动设备上的标准网站存在其他问题,而不仅仅是屏幕尺寸。 However, I think there is an advantage to this kind of detection. 但是,我认为这种检测有一个优势。 If your only issue is the screen size, I think it is a good way to go. 如果您唯一的问题是屏幕尺寸,我认为这是一个很好的方法。

It depends on what you want to achieve. 这取决于你想要达到的目标。

If you design for different screen resolutions regardless of device type then it is fine to use resolution ranges. 如果您根据设备类型设计不同的屏幕分辨率,则可以使用分辨率范围。

If you design for specific device types (phone, tablet, etc.) and assume a resolution range will always match a single device type , then it will eventually break. 如果您针对特定设备类型(手机,平板电脑等)进行设计并假设分辨率范围始终与单个设备类型匹配 ,那么它最终会中断。

You used a 400px threshold in your example, the Galaxy S8+ reports 412x846 with this code: 您在示例中使用了400px阈值,Galaxy S8 +使用以下代码报告412x846

 console.log("width: " + screen.width + ", height: " + screen.height); 

Device resolutions change every year and they are starting to overlap with each other. 设备分辨率每年都在变化,并且它们开始相互重叠。 Large phones have higher resolutions than small tablets and large tablets have higher resolution than some desktops. 大型手机的分辨率高于小型平板电脑,大型平板电脑的分辨率高于某些台式机。

You may get away with it if you just want it to mostly work or if you want to detect specific phones. 如果您只是希望它主要工作或者您想要检测特定的手机,那么您可能会侥幸成功。

However it is not reliable to use screen resolution alone to detect the device type. 但是, 单独使用屏幕分辨率来检测设备类型是不可靠的。

Probably not going to hurt to add this functionality to your website for those who are indeed running JavaScript enabled web browsers on their mobile devices. 对于那些确实在其移动设备上运行支持JavaScript的Web浏览器的人来说,将此功能添加到您的网站可能不会有什么坏处。 As for those who are not, well there's little you can do about them, other than something simple like letting them select their screen size at first load? 对于那些没有的人,除了让他们在第一次加载时选择屏幕大小这样简单的东西之外,你能做些什么呢? Maybe a simple drop down list with possible sizes? 也许一个简单的下拉列表可能有大小?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM