Created
April 28, 2023 11:22
-
-
Save luckylooke/6969315493856823a6a1008669775af2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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