7

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.

Thanks,

Shadi

4

1 回答 1

19

检测移动设备:

  1. 简单的浏览器嗅探

    if (/mobile/i.test(navigator.userAgent)) {...}
    
  2. jQuery.browser.mobile插件(详尽的浏览器嗅探)

  3. 触摸事件的简单测试

    if ('ontouchstart' in window) {...}
    
  4. 触摸事件的高级测试:

    if (('ontouchstart' in window) ||     // Advanced test for touch events
       (window.DocumentTouch && document instanceof DocumentTouch) ||
       ((hash['touch'] && hash['touch'].offsetTop) === 9)) {...}
    

可选择onorientationchange用于上面的#3 和#4。

根据需要组合其中的一种或多种(以及任何其他方法)。它们都不是万无一失的。

于 2012-12-10T16:47:35.270 回答