检测设备是否能够在JavaScript中更改方向 [英] Detecting if a device is able to change orientation in JavaScript
问题描述
是否存在本机JavaScript(或通过使用诸如JQuery / Modernizr之类的库等)检测设备是否能够更改方向?我想用它作为区分桌面和移动设备的方法。
Is there a native JavaScript (or through the use of a library such as JQuery/Modernizr etc) way to detect if a device is capable of changing orientation? I would like to use that as a method to differentiate between desktop and mobile.
谢谢,
Shadi
推荐答案
检测移动设备:
-
简单的浏览器嗅探
Simple browser sniffing
if (/mobile/i.test(navigator.userAgent)) {...}
jQuery.browser.mobile 插件(详尽的浏览器嗅探)
jQuery.browser.mobile plug-in (exhaustive browser sniffing)
触摸事件的简单测试
if ('ontouchstart' in window) {...}
触摸事件的高级测试:
Advanced test for touch events:
if (('ontouchstart' in window) || // Advanced test for touch events
(window.DocumentTouch && document instanceof DocumentTouch) ||
((hash['touch'] && hash['touch'].offsetTop) === 9)) {...}
<小时>
可选择使用上面#3和#4的 onorientationchange
。
根据需要将这些(以及任何其他方法)中的一个或多个组合在一起。它们都不是万无一失的。
Combine 1 or more of these (and any other approaches) as needed. None of them are foolproof.
这篇关于检测设备是否能够在JavaScript中更改方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!