Skip to content

Instantly share code, notes, and snippets.

@pigeonflight
Last active February 9, 2019 13:32
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 pigeonflight/733276299fdd0faea7d8678ec35f7359 to your computer and use it in GitHub Desktop.
Save pigeonflight/733276299fdd0faea7d8678ec35f7359 to your computer and use it in GitHub Desktop.
Hover Menu Fix for Mobile Devices - disable menu hover on touch devices (Vanilla JS implementation)

I wanted a solution that was implemented in vanilla js and disabled hovers on the mobile devices. You will need to change the selector to match that of the target you have in mind.

function is_touch_device() {
return !!('ontouchstart' in window);
}
document.addEventListener("DOMContentLoaded", function() {
/* If mobile browser, prevent click on parent nav item from redirecting to URL */
if(is_touch_device()) {
// replace the selector with the one that matches your menu item
var selector = "a.gl-menu-link.dropdown-toggle";
var menulinks = [].slice.call(document.querySelectorAll(selector));
for (menulink of menulinks){
menulink.addEventListener('click', function(event){
event.preventDefault();
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment