Skip to content

Instantly share code, notes, and snippets.

@jonesmac
Created May 16, 2016 14:18
Show Gist options
  • Select an option

  • Save jonesmac/f8a6585d14b0d190d39a8943731a40ec to your computer and use it in GitHub Desktop.

Select an option

Save jonesmac/f8a6585d14b0d190d39a8943731a40ec to your computer and use it in GitHub Desktop.

Revisions

  1. jonesmac created this gist May 16, 2016.
    44 changes: 44 additions & 0 deletions mobileMenuTouchHelper.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    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;