The navigation menu I'm working on has the default CSS behavior (for those rare people who have JavaScript disabled). By default, the submenu is not displayed:
.main-navigation ul ul { display:none; }
When hovering, the submenu is indicated:
.main-navigation ul li:hover > ul { display:block; }
For most JavaScript-oriented users, the menu is socked with the following jQuery snippet:
jQuery(document).ready(function($) { $('.main-navigation ul li').on('mouseover',function(){ $('.main-navigation ul li:hover > ul').css('display', 'none'); $(this).css('cursor', 'pointer'); }); $('.main-navigation ul li a').click(function() { var li = $(this).closest('li'); if(li.has('ul')) li.find('ul').slideToggle(100); }); });
This switch works fine, except that it only works as long as the mouse cursor remains over the parent link. If the submenu is open and the user moves the mouse from the parent link, the submenu closes.
Question: How to keep a submenu open during mouse output, if it is already open?
I tried adding something like this to my jQuery fragment:
$('.main-navigation ul li').on('mouseout',function(){ if ($('.main-navigation ul li ul').css('display') = 'none') { $('.main-navigation ul li ul').css('display', 'none'); } else if ($('.main-navigation ul li ul').css('display') = 'block') { $('.main-navigation ul li ul').css('display', 'block'); } });
Not only is this mediocre coding, but it actually doesn't work .; - (
How do I fix this problem?
Thanks in advance for your suggestions!