apamphilon

Mobile sub-menu toggle

// mobile sub-menu toggle

touchDropdowns(); // init

function touchDropdowns() {
  var nav = $('#mastnav');
  var itemHasChildren = $('#mastnav .menu-item-has-children');

  itemHasChildren.children('a').on('click', function(e) {
    var width = (window.innerWidth !== undefined) ? window.innerWidth : document.documentElement.clientWidth;
    var item = $(this).parent();

    if (!item.hasClass('active') && width <= 959) {
      e.preventDefault();
      nav.find('.active').removeClass('active').find('.sub-menu').stop().slideUp(350);
      item.addClass('active');
      item.find('.sub-menu').stop().slideToggle(350);
    }
  });
};

or to allow support for sub-menu two levels deep

// mobile sub-menu toggle
touchDropdowns(); // init

function touchDropdowns() {
  var nav = $('.main-navigation');
  var itemHasChildren = $('.main-navigation .menu-item-has-children');

  itemHasChildren.children('a').on('click', function(e) {
    var width = (window.innerWidth !== undefined) ? window.innerWidth : document.documentElement.clientWidth;
    var item = $(this).parent();

    if (!item.hasClass('active') && width <= 768 && !item.parent('ul').hasClass('sub-menu')) {
      e.preventDefault();
      nav.find('.active').parent('ul').not('.sub-menu').find('.sub-menu').stop().slideUp(350);
      nav.find('.active').removeClass('active');
      item.not('#menu-item-193').addClass('active');
      item.find('.sub-menu').stop().slideDown(350);
    }
  });
};

Comments: