问题
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