Detecting if a device is able to change orientation in JavaScript

后端 未结 1 999
误落风尘
误落风尘 2020-12-03 09:28

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

相关标签:
1条回答
  • 2020-12-03 09:48

    Detecting mobile devices:

    1. Simple browser sniffing

      if (/mobile/i.test(navigator.userAgent)) {...}
      
    2. jQuery.browser.mobile plug-in (exhaustive browser sniffing)

    3. Simple test for touch events

      if ('ontouchstart' in window) {...}
      
    4. 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)) {...}
      

    Optionally use onorientationchange for #3 and #4 above.

    Combine 1 or more of these (and any other approaches) as needed. None of them are foolproof.

    0 讨论(0)
提交回复
热议问题