Skip to content

Instantly share code, notes, and snippets.

@jonesmac
Created May 16, 2016 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jonesmac/f8a6585d14b0d190d39a8943731a40ec to your computer and use it in GitHub Desktop.
Save jonesmac/f8a6585d14b0d190d39a8943731a40ec to your computer and use it in GitHub Desktop.
Touch Friendly Drop Down Menu
function mobileMenuTouchHelper () {
$(document).on('page:change', function () {
var topLevelMenuItems = $('.menu__list > a');
menuTouches(topLevelMenuItems);
$(window).on('orientationchange', function () {
unbindTouches(topLevelMenuItems);
menuTouches(topLevelMenuItems);
});
});
}
function unbindTouches (items) {
$.each(items, function (index, elem) {
$(elem).unbind('touchend');
});
}
function menuTouches (items) {
if ($(window).width() > 767) {
$.each(items, function (index, elem) {
$(elem).on('touchend', menuTouchHandler.bind(this, items));
});
}
}
function menuTouchHandler (items, e) {
if ($(this).parent().hasClass('js-menu_hover')) {
$(this).parent().removeClass('js-menu_hover');
} else {
e.preventDefault();
$(this).parent().addClass('js-menu_hover');
removeOthers(this, items);
}
}
function removeOthers (currentElem, items) {
$.each(items, function (index, elem) {
if ($(currentElem).text() !== $(elem).text()) {
$(this).parent().removeClass('js-menu_hover');
}
});
}
module.exports = mobileMenuTouchHelper;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment