Created
April 26, 2020 15:56
-
-
Save ricardocanelas/724c6b0dee40197fb9a7a6cbd4afb4cd to your computer and use it in GitHub Desktop.
isTouchDevice
This file contains hidden or 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
const isTouchDevice = () => { | |
const prefixes = ['', '-webkit-', '-moz-', '-o-', '-ms-', '']; | |
const mq = query => window.matchMedia(query).matches; | |
if ( | |
'ontouchstart' in window || | |
(window.DocumentTouch && document instanceof 'DocumentTouch') | |
) { | |
return true; | |
} | |
return mq(['(', prefixes.join('touch-enabled),('), 'heartz', ')'].join('')); | |
}; | |
if (isTouchDevice()) { | |
document.body.classList.add("touch"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From https://stackoverflow.com/questions/4817029/whats-the-best-way-to-detect-a-touch-screen-device-using-javascript