Wrong (maxTouchPoints) and ('ontouchstart' in document) in Chrome mobile emulation mode

早过忘川 提交于 2020-12-04 07:57:20

问题


I use a touchscreen device detection like this:

if (window.navigator.maxTouchPoints || 'ontouchstart' in document)  
    // handle as mobile device
else
    // handle as desktop

When I change the screen in Chrome mobile emulation the result of both maxTouchPoints and 'ontouchstart' in document is unpredictable.

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?


回答1:


How about this?

function isTouchscreen() {
  return ("ontouchstart" in window) || (navigator.maxTouchPoints > 0) ? true : false;
}

if (isTouchscreen()) {
  console.log('IS touchscreen');}
else {
  console.log('IS NOT touchscreen');
}

Reference: https://developer.mozilla.org/en-US/docs/Web/API/Navigator/maxTouchPoints



来源:https://stackoverflow.com/questions/55833326/wrong-maxtouchpoints-and-ontouchstart-in-document-in-chrome-mobile-emulati

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!