Chrome移动仿真模式下的(maxTouchPoints)和(文档中的"ontouchstart")错误 [英] Wrong (maxTouchPoints) and ('ontouchstart' in document) in Chrome mobile emulation mode
问题描述
我使用像这样的触摸屏设备检测:
I use a touchscreen device detection like this:
if (window.navigator.maxTouchPoints || 'ontouchstart' in document)
// handle as mobile device
else
// handle as desktop
当我在Chrome移动仿真中更改屏幕时,maxTouchPoints
和'ontouchstart' in document
的结果都是不可预测的.
When I change the screen in Chrome mobile emulation the result of both maxTouchPoints
and 'ontouchstart' in document
is unpredictable.
对于一个相同的模拟屏幕,它可能返回maxTouchPoints
等于0或1,并且'ontouchstart' in document
等于true
或false
.
For one and same emulated screen it may return maxTouchPoints
equals to 0 or 1, and 'ontouchstart' in document
equals to true
or false
.
所以,我真的不能接受这项检查.
您能推荐一种解决方法吗?
So, I cannot really on this check.
Could you recommend a way to fix this?
推荐答案
如何?
function isTouchscreen() {
return ("ontouchstart" in window) || (navigator.maxTouchPoints > 0) ? true : false;
}
if (isTouchscreen()) {
console.log('IS touchscreen');}
else {
console.log('IS NOT touchscreen');
}
参考: https://developer.mozilla.org /en-US/docs/Web/API/Navigator/maxTouchPoints
这篇关于Chrome移动仿真模式下的(maxTouchPoints)和(文档中的"ontouchstart")错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!