【问题标题】:How to check for a device/browser that fully supports the deviceorientation event?如何检查完全支持 deviceorientation 事件的设备/浏览器?
【发布时间】:2013-09-24 10:07:55
【问题描述】:

我有一个工作正常的 HTML5 指南针。现在我需要一种聪明的方法来区分“启用伪设备方向”的浏览器(如桌面 chrome 和 FF)和真正的候选者,如使用设备磁力计的 iPhone/Android/iPad 浏览器。

我目前的解决方案是对 DeviceOrientationEvent 和触摸支持进行基本检查:

if (window.DeviceOrientationEvent && 'ontouchstart' in window) {
  // setup real compass thing, with event.alpha
} else {
  // setup some mouse following hack
}

够了吗?我不太确定“具有触摸支持但没有磁力计”的设备,例如 Mac 书上的 chrome 具有触摸支持,以及假的设备方向?

【问题讨论】:

  • if(window.innerHeight > window.innerWidth){ alert("横向方向!"); } 也可以参考this url
  • 为了测试触摸支持,您可以使用 function is_touch_device() { try { document.createEvent("TouchEvent");返回真; } 捕捉 (e) { 返回假; } }
  • 谢谢,但这与改变方向无关。它是关于“它是否内置了真正的指南针”。请参阅developer.mozilla.org/de/docs/WebAPI/… 底部的示例适用于我的 chrome on mac book ...
  • 'ontouchstart' 与设备方向有什么关系?
  • 尝试以某种方式检查设备是否内置了真正的指南针。

标签: javascript html mobile magnetometer


【解决方案1】:

它似乎按原样工作,我错了,mac book 上的 chrome 没有进入“真正的指南针”部分。也许应该测试一下。

if (window.DeviceOrientationEvent && 'ontouchstart' in window) {
    // setup real compass thing, with event.alpha
    document.body.innerHTML = "haz!";
} else {
    // setup some mouse following hack
    document.body.innerHTML = "nope";
}

检查自己:http://jsfiddle.net/benzkji/J58ef/

如果显示启用了设备方向的 chrome 的启用触摸的 windows 笔记本电脑,仍然会发生什么情况。可能检查设备方向事件的“绝对”属性:https://developer.mozilla.org/de/docs/WebAPI/Detecting_device_orientation

【讨论】:

  • 并且可能使用modernizr,以获得更理智的触摸功能检测。
猜你喜欢
  • 2012-12-30
  • 1970-01-01
  • 2017-01-21
  • 1970-01-01
  • 2014-03-27
  • 2020-01-04
  • 2018-06-01
  • 1970-01-01
相关资源
最近更新 更多