I have a little issue here,
I have a menu, and I want to display the submenu inside a div with class .submenuWrap. If the menu item has submenu, It will display the submenu in this .submenuWrap, if the menu item does not have submenu, the .submenuWrap won't show at all.
What I can't work out how to do is to keep this .submenuWrap showing while I am moving around the submenu, as at the moment, as soon as I leave the navigation, it disappears.
I put everything in a jsfiddle as it is much easier to follow:
html
<nav id="mainNav" role="navigation">
<div class="container">
<div id="mainMenu" class="collapse navbar-collapse">
<ul class="nav">
<li><a href="#" class="dropdown-toggle" data-toggle="dropdown">Buy Wine</a>
<div class="dropdown-menu">
<div class="col-sm-3 col-xs-12">
<dl>
<dt><a href="#">By Country</a></dt>
<dd><a href="#" data-abbr="ar">Argentina</a></dd>
<dd><a href="#" data-abbr="au">Australia</a></dd>
<dd><a href="#" data-abbr="it">Italy</a></dd>
<dd><a href="#" data-abbr="fr">France</a></dd>
<dd><a href="#" data-abbr="es">Spain</a></dd>
<dd><a href="#" data-abbr="gb">United Kingdom</a></dd>
</dl>
</div>
<div class="col-sm-3 col-xs-12">
<dl>
<dt><a href="#">By Type</a></dt>
<dd><a href="#">Red</a></dd>
<dd><a href="#">White</a></dd>
<dd><a href="#">Rose</a></dd>
<dd><a href="#">Sparkling</a></dd>
<dd><a href="#">Port</a></dd>
</dl>
</div>
<div class="col-sm-3 col-xs-12">
<dl>
<dt><a href="#">By Something</a></dt>
<dd><a href="#">Red</a></dd>
<dd><a href="#">White</a></dd>
<dd><a href="#">Rose</a></dd>
<dd><a href="#">Sparkling</a></dd>
<dd><a href="#">Port</a></dd>
</dl>
</div>
<div class="col-sm-3 col-xs-12">
<dl>
<dt><a href="#">By Price</a></dt>
<dd><a href="#">£0 - £50</a></dd>
<dd><a href="#">£50 - £100</a></dd>
<dd><a href="#">£100 - £200</a></dd>
<dd><a href="#">£200 - £500</a></dd>
<dd><a href="#">£500+</a></dd>
</dl>
</div>
</div>
</li>
<li><a href="#" class="dropdown-toggle" data-toggle="dropdown">en primeur</a>
<div class="dropdown-menu">
<div class="col-sm-6 col-xs-12">
<dl>
<dt><a href="#">PIRULO</a></dt>
<dd><a href="#" data-abbr="ar">Argentina</a></dd>
<dd><a href="#" data-abbr="au">Australia</a></dd>
<dd><a href="#" data-abbr="it">Italy</a></dd>
<dd><a href="#" data-abbr="fr">France</a></dd>
<dd><a href="#" data-abbr="es">Spain</a></dd>
<dd><a href="#" data-abbr="gb">United Kingdom</a></dd>
</dl>
</div>
<div class="col-sm-6 col-xs-12">
<dl>
<dt><a href="#">PIRULA</a></dt>
<dd><a href="#">Red</a></dd>
<dd><a href="#">White</a></dd>
<dd><a href="#">Rose</a></dd>
<dd><a href="#">Sparkling</a></dd>
<dd><a href="#">Port</a></dd>
</dl>
</div>
</div>
</li>
<li><a href="#">latest offers</a></li>
<li><a href="#">what's new</a></li>
<li><a href="#">our blog</a></li>
<li><a href="#">services</a></li>
</ul>
</div>
<div class="submenuWrap"></div>
</div>
</nav>
css
#mainNav {
background-color: $white;
border-top: 5px solid black;
border-bottom: 5px solid black;
}
#mainMenu > ul {
display: table;
width: 100%;
margin-bottom: 0;
}
#mainMenu > ul > li {
display: table-cell;
}
#mainMenu > ul > li > a {
text-align: center;
}
.submenuWrap {
position: absolute;
background-color: khaki;
width: 98%;
border-left: 5px solid #333333;
border-right: 5px solid #333333;
background-color: $white;
margin: 0 auto;
top: 50px;
right: 0;
left: 0;
border-top: 0;
display:block;
z-index:6;
}
js
var submenu = $(this).next(".dropdown-menu").html();
$( "#mainMenu .dropdown-toggle" ).on({
mouseover: function() {
submenu = $(this).next(".dropdown-menu").html();
}, mouseenter: function() {
$(".submenuWrap").html(submenu).css("border-bottom", "5px solid black");
}, mouseleave: function() {
$(".submenuWrap").html("").css("border-bottom", "0");
}
});
I have tried adding this:
$( ".submenuWrap" ).on({
mouseover: function() {
$(this).css("display", "block");
}, mouseleave: function() {
$(this).css("display", "none");
}
});
But it does nothing, and I don't understand why either.
https://jsfiddle.net/yLpLfp9w/
Does anyone can give me a hand?
Thank you
There's something wrong with your jsfiddle code, the menu html isn't showing, just a black bar, so here's a great example on CSS tricks here for how a jQuery drop down menu can work: https://css-tricks.com/examples/SimplejQueryDropdowns/
With code here: http://css-tricks.com/examples/SimplejQueryDropdowns.zip
I'll create a fiddle with the above code here:
The above code adds a class to the top level ul called "hover", and this way you can use css to keep the first submenu open. Hovering on a submenu ul then changes its visibility to "visible" while keeping the class "hover" on the top level ul.
You can add hoverIntent which delays opening the menu a little, stopping accidental menu opens, but this isn't required.
In your case, make sure the top level ul gets its "hover" class removed only when you hover away from that element and its children (again, see the example I posted).