Skip to content

Instantly share code, notes, and snippets.

@ricardocanelas
Created April 26, 2020 15:56
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 ricardocanelas/724c6b0dee40197fb9a7a6cbd4afb4cd to your computer and use it in GitHub Desktop.
Save ricardocanelas/724c6b0dee40197fb9a7a6cbd4afb4cd to your computer and use it in GitHub Desktop.
isTouchDevice
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