检测设备是否能够在JavaScript中更改方向 [英] Detecting if a device is able to change orientation in JavaScript

查看:208
本文介绍了检测设备是否能够在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

推荐答案

检测移动设备:


  1. 简单的浏览器嗅探

  1. 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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆