I have items contained in an unordered list. I have hidden divs (using display:none;) outside of this list. When you hover over a list item, I would like the div to appear. It must also remain visible if you move your mouse from the list item to the div below it.
I am open to accomplish this through CSS, JavaScript/jQuery. Also, my document is set to HTML5.
This is what I am currently working with.
<nav id="main-menu">
<ul>
<li><a class="first-link parent" href="">First Link</a></li>
<li><a class="second-link parent" href="">Second Link</a></li>
<li><a class="third-link parent" href="">Third Link</a></li>
<li><a class="fourth-link parent" href="">Fourth Link</a></li>
</ul>
</nav><!-- #main-menu !-->
And beneath the nav, I have the content that I would like to display when hovering over the corresponding item.
<div id="first-widget" class="widget-container">
<h2>Sub Title</h2>
<p>Lorem Ipsum
<a href="#">Read More</a></p>
</div><!-- .first-widget !-->
<div id="second-widget" class="widget-container">
<h2>Sub Title</h2>
<p>Lorem Ipsum
<a href="#">Read More</a></p>
</div><!-- .second-widget !-->
<div id="third-widget" class="widget-container">
<h2>Sub Title</h2>
<p>Lorem Ipsum
<a href="#">Read More</a></p>
</div><!-- .third-widget !-->
<div id="fourth-widget" class="widget-container">
<h2>Sub Title</h2>
<p>Lorem Ipsum
<a href="#">Read More</a></p>
</div><!-- .fourth-widget !-->
</aside><!-- .hidden !-->
I feel like there should be an easy solution to this, but my attempts so far have fallen short. The biggest problem is having the div show not only when hovering the menu item, but also when you move from the menu to the visible div.
Any advice is appreciate, thank you for taking the time to read this far.
You can wrap the widgets in a div
and attach a mouseleave
event to it. This solution also uses the rel attibute
to store the widget id
: DEMO
HTML
<div id="wrap">
<nav id="main-menu">
<ul>
<li><a class="first-link parent" rel="first-widget" href="">First Link</a></li>
<li><a class="second-link parent" rel="second-widget" href="">Second Link</a></li>
<li><a class="third-link parent" rel="third-widget" href="">Third Link</a></li>
<li><a class="fourth-link parent" rel="fourth-widget" href="">Fourth Link</a></li>
</ul>
</nav><!-- #main-menu !-->
<div id="first-widget" class="widget-container">
<h2>Sub Title 1</h2>
<p>Lorem Ipsum
<a href="#">Read More</a></p>
</div><!-- .first-widget !-->
...
</div>
JS
$('#main-menu a').mouseover(function() {
var $this = $(this);
var id = $this.attr('rel');
var $currentWidget = $('#' + id);
$currentWidget.show().siblings('.widget-container').hide();
});
$('#wrap').mouseleave(function() {
$('.widget-container').hide();
});