我有一个 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 book 上的 chrome 具有触摸支持和虚假的设备方向?
最佳答案
它似乎按原样工作,我错了,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/
如果显示启用了 deviceorientation 的 chrome 的触摸启用 windos 笔记本电脑,仍然会发生什么。可能检查设备方向事件的“绝对”属性:https://developer.mozilla.org/de/docs/WebAPI/Detecting_device_orientation
关于javascript - 如何检查完全支持 deviceorientation 事件的设备/浏览器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18892719/