jQuery Menus Acting Buggy on Mobile Devices
I'm working on a test site which can be found here: https://devwww.interfacett.com
The menu functionality works fine at any size on a desktop computer, but when I switch to my iPhone the mobile menu gets really buggy. Notably when you click open one of the accordion menus and then use your finger to scroll down the page, it automatically closes the menu.
Try it on your phone and you'll see what I mean.
The mobile menus uses this code for the functionality:
jQuery(document).ready(function(){
-
- function resizeForm(){
- var width = (window.innerWidth > 0) ? window.innerWidth : document.documentElement.clientWidth;
- if(width > 1024){
-
- } else {
-
-
- function accordion_menus(){
-
- // if we are not on mobile (menu icon is hidden) show sub items and bail
- console.log('debug: start');
- if ( jQuery('#primary-navigation .menu-toggle').is(':hidden') ){
-
- console.log('debug: yes, it is hidden');
- // show sub menus
- $('#primary-navigation ul.nav-menu ul.sub-menu').show();
- return;
- } else{
- // hide sub menus
- $('#primary-navigation ul.nav-menu ul.sub-menu').hide();
- }
-
- // top level nav click function
- $('#primary-navigation ul.nav-menu > li > a').click(function(e){
-
- // store parent li to variable
- var parent_li = $(this).parent('li');
-
- // if sub menu does not exist in parent li
- if ( !$('ul.sub-menu', parent_li).first().length ) {
- return;
- }
-
- // if sub menu is already active, bail
- if ( parent_li.hasClass('sub-menu-active') ){
- parent_li.find('ul').slideUp(100, function(parent_li){
- });
- parent_li.removeClass('sub-menu-active');
-
- return false;
- }
-
- // stop link click
- e.preventDefault();
-
- // store current sub menu in variable
- var current_submenu = $('ul.sub-menu', parent_li).first();
-
- // slide up non-current sub menus
- $('#primary-navigation').find('ul.sub-menu').not(current_submenu).slideUp(function(parent_li){
-
- // remove sub-menu-active class from all first level items except current parent li
- $('#primary-navigation').find('li').not(parent_li).removeClass('sub-menu-active');
-
- });
-
- // slide down current sub menu
- current_submenu.slideDown(100, function(){
- // add sub-menu-active to current parent li
- parent_li.addClass('sub-menu-active');
- });
-
- });
-
- // second level nav click function
- jQuery('#primary-navigation ul.nav-menu ul.sub-menu > li > a').click(function(e){
-
- // store parent li to variable
- var parent_li = jQuery(this).parent('li');
-
- // if sub menu does not exist in parent li
- if ( !jQuery('ul.sub-menu', parent_li).first().length ) {
- return;
- }
-
- // if sub menu is already active, bail
- if ( parent_li.hasClass('sub-menu-active') ){
- parent_li.find('ul').slideUp(100, function(){
-
- // remove sub-menu-active class from all first level items except current parent li
- });
- parent_li.removeClass('sub-menu-active');
- return false;
- }
-
- // stop link click
- e.preventDefault();
-
- // store current sub menu in variable
- var current_submenu = jQuery('ul.sub-menu', parent_li).first();
-
- // slide up non-current sub menus
- jQuery('#primary-navigation ul.nav-menu ul.sub-menu > li > ul.sub-menu').not(current_submenu).slideUp(function(){
-
- // remove sub-menu-active class from all second level items except current parent li
- jQuery('#primary-navigation ul.nav-menu ul.sub-menu > li').not(parent_li).removeClass('sub-menu-active');
-
- });
-
- // slide down current sub menu
- current_submenu.slideDown(100, function(){
- // add sub-menu-active to current parent li
- parent_li.addClass('sub-menu-active');
- });
-
- });
-
- }
-
- // load menu accordion on doc ready
- jQuery(document).ready(function($) {
- accordion_menus();
- });
-
- // load menu accordion on window resize
- jQuery(window).resize(function(){
- accordion_menus();
- });
-
-
- }
- }
- window.onresize = resizeForm;
- resizeForm();
- });
I'm not sure what I'm doing wrong here, but I'm hoping someone can chime in and tell me what I'm missing. I can't seem to figure it out.
Thanks in advance, I really appreciate it!