Skip to content

Instantly share code, notes, and snippets.

@luckylooke
Created April 28, 2023 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luckylooke/6969315493856823a6a1008669775af2 to your computer and use it in GitHub Desktop.
Save luckylooke/6969315493856823a6a1008669775af2 to your computer and use it in GitHub Desktop.
// Mouse vs touch detection, supporting devices which are capable of both
// by default set using touch for all touch enabled devices
let userUsesTouch =
'ontouchstart' in document.documentElement ||
navigator.maxTouchPoints > 0 ||
navigator.msMaxTouchPoints > 0;
if (userUsesTouch) {
catchMouse();
} else {
catchTouch();
}
function catchTouch() {
document.addEventListener(
'touchstart',
() => {
userUsesTouch = false;
catchTouch();
},
{ once: true }
);
}
function catchMouse() {
document.addEventListener(
'mousemove',
() => {
userUsesTouch = false;
catchTouch();
},
{ once: true }
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment