javascript - Remove mouseenter from an element then restore it (for a jQuery dropdown nav) -
i'm trying create drop down menu using jquery.
i've got working problem can still hover on subnav that's sliding out while new 1 sliding in. results in menu flashing between 2 because mouseenter encompasses of child elements.
i need way remove mouseenter subnav ul while it's animating restore once it's done. works fine if change .slideup(300, 'swing') .css('display', 'none') disappears abruptly.
update: set link can see effect. http://samlester.net/nav/
my html in following format:
<nav id="main-nav"> <ul> <li><a href="#" id="home" class="current">home</a></li> <li> <a href="#">page 1</a> <ul id="page1-subnav"> <li><a href="#">page 1-1</a></li> <li><a href="#">page 1-2</a></li> </ul> </li> <li><a href="#">page 2</a></li> </ul> </nav> and here's javascript i've got far...
$(document).ready(function() { var nav = $("#main-nav"); nav.find("li").each(function() { if ($(this).find("ul").length > 0) { //show submenus on hover $(this).mouseenter(function() { $(this).find("ul").stop(true, true).slidedown(300, 'swing'); }); //hide submenus on exit $(this).mouseleave(function() { $(this).find("ul").stop(true, true).slideup(300, 'swing'); }); } }); }); thanks, sam
try - same concept before logic plugged in.
html:
<nav id="main-nav"> <ul> <li><a href="#" id="home" class="current">home</a></li> <li> <a href="#">page 1</a> <ul id="page1-subnav"> <li><a href="#">page 1-1</a></li> <li><a href="#">page 1-2</a></li> </ul> </li> <li><a href="#">page 2</a></li> <li> <a href="#">page 3</a> <ul id="page3-subnav"> <li><a href="#">page 3-1</a></li> <li><a href="#">page 3-2</a></li> </ul> </li> <li> <a href="#">page 4</a> <ul id="page4-subnav"> <li><a href="#">page 4-1</a></li> <li><a href="#">page 4-2</a></li> </ul> </li> </ul> </nav> css:
#main-nav ul li ul {display:none;} js:
$(document).ready(function() { var nav = $("#main-nav"); nav.find("li").each(function() { if ($(this).find("ul").length > 0) { //show submenus on hover $(this).mouseenter(function(event) { event.stoppropagation(); if($(this).children('ul').data('interactive') !== false) { $(this).children('ul').data('interactive',false); $(this).find("ul").stop(true, true).slidedown(300, 'swing', function(){$(this).data('interactive',true)}); } }); //hide submenus on exit $(this).mouseleave(function(event) { event.stoppropagation(); // no if statement here - force close on mouseleave $(this).children('ul').data('interactive',false); $(this).find("ul").stop(true, true).slideup(300, 'swing', function(){$(this).data('interactive',true)}); }); } }); });
Comments
Post a Comment