I just started coding in jQuery last week and you need help on how to properly configure the menu. I have 3 tabs each with its own menu. When the page is displayed, the menu and submenu are automatically displayed. After it is displayed, the user can hover over the tabs to see other submenus, and when they stop hanging, a submenu will initially appear.
My problem is that although I can show them the submenu of other tabs, I cannot leave the submenu open for the user to click on the submenu item. Other tutorials show how to do this only when the submenu is nested in the parent element, but my code for the menu structure does not have submenus (this is how the code was when I joined the project). Is there a way to keep an open submenu if the user hovers over the corresponding tab?
Here is my HTML menu:
<div id="navigation"> <div id="main-nav"> <div id="kids"><a href="../images/nav1.png"></a></div> <div id="community"><a href="../images/nav2.png"></a></div> <div id="about"><a href="../images/nav3.png"></a></div> </div> </div> <div id="sub-nav"> <ul class="menu-1 requiresJS"> <li><a href="#">Item1</a></li> <li><a href="#">Item2</a></li> <li><a href="#">Item3</a></li> <li><a href="#">Item4</a></li> <li><a href="#">Item5</a></li> <li><a href="#">Item6</a></li> </ul> <ul class="menu-2 requiresJS"> <li><a href="#">Item1</a></li> <li><a href="#">Item2</a></li> <li><a href="#">Item3</a></li> <li><a href="#">Item4</a></li> <li><a href="#">Item5</a></li> <li><a href="#">Item6</a></li> </ul> <ul class="menu-3 requiresJS"> <li><a href="#">Item1</a></li> <li><a href="#">Item2</a></li> <li><a href="#">Item3</a></li> <li><a href="#">Item4</a></li> <li><a href="#">Item5</a></li> <li><a href="#">Item6</a></li> </ul>
Here is my jQuery:
// For JS users, display sub menus by default $(".requiresJS").css("display","block"); var startMenu //hide all sub menus $("#sub-nav ul").hide(); // check URL for about, community or kids and set startMenu with correct term if(window.location.href.indexOf("about") != -1){startMenu = "about"} else if(window.location.href.indexOf("community") != -1){startMenu = "community"} else{startMenu = "kids"} // highlight correct category tab $("div#main-nav #" + startMenu).addClass("selected"); // show correct starting menu $("#sub-nav ul.menu-" + startMenu).show('slide', {direction: 'right'}, 600).effect("bounce", { times:1,direction:"right",distance:13 }, 300); // show correct menu on mouseover of div $("div#main-nav div").mouseover(function() { $("#sub-nav ul").stop(true, true) $("#sub-nav ul").hide(); var currentId = $(this).attr('id'); $("#sub-nav ul.menu-" + currentId).show(); }); $("div#main-nav div").mouseover(function() { $("#sub-nav ul").stop(true, true) $("#sub-nav ul").hide(); var currentId = $(this).attr('id'); $("#sub-nav ul.menu-" + currentId).show(); });
source share