Skip to content

Instantly share code, notes, and snippets.

@joshapgar
Last active March 5, 2019 15:58
Show Gist options
  • Save joshapgar/860c2fc6e5c5b7a83f8aa8d0b39e4d5f to your computer and use it in GitHub Desktop.
Save joshapgar/860c2fc6e5c5b7a83f8aa8d0b39e4d5f to your computer and use it in GitHub Desktop.
Function for detecting touch device and adding class to menu items when clicked with touch device.
function is_touch_device() {
var prefixes = ' -webkit- -moz- -o- -ms- '.split(' ');
var mq = function(query) {
return window.matchMedia(query).matches;
}
if (('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch) {
return true;
}
var query = ['(', prefixes.join('touch-enabled),('), 'heartz', ')'].join('');
return mq(query);
}
var isTouch = is_touch_device();
if(isTouch) {
$(".menu-item-has-children").click(function(e) {
if(!$(this).hasClass("mobile-touch")){
e.preventDefault();
$(".menu-item-has-children").removeClass("mobile-touch");
$(this).addClass("mobile-touch");
} else {
return true;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment