问题描述
我有一个运行正常的HTML5指南针.现在,我需要一种聪明的方法来区分启用了伪设备定向"的浏览器(如桌面chrome和FF)和使用该设备的磁阻计的iPhone/Android/iPad浏览器之类的真实候选对象.
I have a HTML5 compass that works quite ok. Now I need a smart way to distinguish between "pseudo deviceorientation enabled" browsers (as desktop chrome and FF) and real candidates like iPhone/Android/iPad Browsers that use the device's magnetometor.
我当前的解决方案是对DeviceOrientationEvent和触摸支持的基本检查:
My current solution is a basic check for the DeviceOrientationEvent and touch support:
if (window.DeviceOrientationEvent && 'ontouchstart' in window) {
// setup real compass thing, with event.alpha
} else {
// setup some mouse following hack
}
够了吗?我不太确定具有触摸支持但没有磁力计"的设备,例如macbook上的Chrome具有触摸支持,并且设备方向是假的吗?
Is that enough? I'm not really sure about devices "with touch support but no magnetometer", as for example chrome on a mac book has touch support, and a fake deviceorientation?
推荐答案
它似乎可以正常工作,我错了,mac book上的chrome不在真正的指南针"部分.应该已经测试过了.
it seems to work as is, I was wrong, chrome on mac book is not going into the "real compass" section. should have tested it, maybe.
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的启用触摸的Windos笔记本电脑.可能检查deviceorientation事件的绝对"属性: https://developer.mozilla.org /de/docs/WebAPI/Detecting_device_orientation
remains what happens if a touch enabled windos laptop with deviceorientation enabled chrome is showing up. probably check the "absolute" property of the deviceorientation event: https://developer.mozilla.org/de/docs/WebAPI/Detecting_device_orientation
这篇关于如何检查完全支持deviceorientation事件的设备/浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!