检测设备是否能够在JavaScript中更改方向
是否存在本机JavaScript(或通过使用诸如JQuery / Modernizr等库)来检测设备是否能够改变方向? 我想将其用作区分桌面和移动设备的方法。
谢谢,
沙迪
检测移动设备:
-
简单的浏览器嗅探
if (/mobile/i.test(navigator.userAgent)) {...}
-
jQuery.browser.mobile插件(详尽的浏览器嗅探)
-
简单测试触摸事件
if ('ontouchstart' in window) {...}
-
触摸事件的高级测试:
if (('ontouchstart' in window) || // Advanced test for touch events (window.DocumentTouch && document instanceof DocumentTouch) || ((hash['touch'] && hash['touch'].offsetTop) === 9)) {...}
可选择使用onorientationchange
#3和#4的onorientationchange
。
根据需要组合这些(和任何其他方法)中的一个或多个。 它们都不是万无一失的。