Chrome移动仿真模式下的(maxTouchPoints)和(文档中的"ontouchstart")错误 [英] Wrong (maxTouchPoints) and ('ontouchstart' in document) in Chrome mobile emulation mode

查看:375
本文介绍了Chrome移动仿真模式下的(maxTouchPoints)和(文档中的"ontouchstart")错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用像这样的触摸屏设备检测:

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等于truefalse.

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

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