Skip to content

Instantly share code, notes, and snippets.

@joonassandell
Last active September 13, 2016 14:46
Show Gist options
  • Save joonassandell/4ce9b660235fd7cd0054 to your computer and use it in GitHub Desktop.
Save joonassandell/4ce9b660235fd7cd0054 to your computer and use it in GitHub Desktop.
Detect touch support
/**
* Detect "touch" support and act accordingly.
* Adds `touch` class and assumes there is `no-touch` class added in <html> element.
*/
let hasTouch = false;
const docEl = document.documentElement;
if (('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch || navigator.msMaxTouchPoints) {
docEl.className = docEl.className.replace(/(^|\s)no-touch(\s|$)/, ' touch ');
hasTouch = true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment