有没有原生JavaScript(或通过使用诸如JQuery / Modernizr等库)的方法来检测设备是否能够更改方向?我想将其用作区分桌面和移动设备的方法。
谢谢,
Shadi
有没有原生JavaScript(或通过使用诸如JQuery / Modernizr等库)的方法来检测设备是否能够更改方向?我想将其用作区分桌面和移动设备的方法。
谢谢,
Shadi
检测移动设备:
简单的浏览器嗅探
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)) {...}
对于上面的#3和#4,可以选择使用onorientationchange
。
根据需要组合1个或多个方法(以及其他任何方法)。 这些方法都不是万无一失的。
onorientationchange
似乎可以成为window
的一个属性。我的浏览器没有它,但它似乎是一个有效的事件 - https://dev59.com/42435IYBdhLWcg3wqB8x - Ian