jDiv: A jQuery navigation menu alternative
February 1, 2010 jQuery Tutorials
Late last year, I was working on the UI for a potential project and I needed a drop-down menu that displayed any content I needed, specifically a combination of images and lists. I didn't feel that only using a UL style was semantic enough or gave me the flexibility I needed. So, jDiv was born.
What's the point?
I needed a specific way to use a drop-down effect for a menu without being limited to only an unordered list and this seemed to fit the bill. I don't necessarily consider this the definitive method for achieving this effect but it got the job done.
The advantages
- More flexibility in design that your usual UL style;
- The use of the H4 tag and a UL for the hidden content adds some potential SEO but be sure that your header tag choice is in keeping with your copy's layout;
- As long as your first-level nav item is linked to a page, you still retain accessibility with JS disabled.
The challenges
- Using more than one instance of the effect requires additional code in the JS. This isn't necessarily a problem unless you want your file sizes next to nothing;
- If your content in the DIV is too heavy it may require a slight (if even noticible) page load time - more of a concern if your overall page is quite content-heavy or makes numerous calls to the server;
Another example of jDiv in use
Peter Hinton has utilized this script with his own modifications which I recommend you check out!
The XHTML markup:
// Place in your HEAD tag: <link href="css/style.css" rel="stylesheet" type="text/css" media="screen" charset="utf-8" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script type="text/javascript" src="js/jdiv.js"></script> // Place in your BODY tag: <!-- BEGIN MAIN NAV MENU--> <div id="menu"> <ul> <li><a href="#" id="menu1">Rollover me!</a></li> <li><a href="#">Link # 2</a><</li> <li><a href="#">Link # 3</a></li> </ul> </div> <!-- END MAIN NAV MENU--> <!-- BEGIN HIDDEN DIV CONTENT --> <!-- NOTE THAT YOU MAY WANT TO PLACE IT INSIDE THE UL ABOVE. I KEEP IT HERE FOR THE PURPOSES OF THIS DEMO. --> <div id="hidden-div"> <div id="hidden-div-left"> <!-- NOTE THAT YOU WILL WANT TO USE A MORE SEMANTIC NAME FOR THESE DIVS. THIS IS FOR THE PURPOSE OF THE DEMO. --> <h4>This is the hidden panel that can display any content you wish.<br/><br/>Perhaps you would like a description here and your section's links to the right.</h4> </div> <div id="hidden-div-right"> <!-- DITTO --> <ul class="submenu"> <li><a href="#">» List item #1</a></li> <li><a href="#">» List item #2</a></li> <li><a href="#">» List item #3</a></li> <li><a href="#">» List item #4</a></li> <li><a href="#">» List item #5</a></li> </ul> </div> </div> <!-- END HIDDEN DIV CONTENT -->
The jQuery:
// DISPLAYS HIDDEN DIV ON HOVER
$(document).ready(function() {
var hide = false;
// Shows the DIV on hover with a fade in
$("#menu1").hover(function(){
if (hide) clearTimeout(hide);
$("#hidden-div").fadeIn();
// The main nav menu item is assigned the 'active' CSS class
$(this).addClass("active");
}, function() {
// Fades out the DIV and removes the 'active' class from the main nav menu item
hide = setTimeout(function() {$("#hidden-div").fadeOut("fast");});
$("#menu1").removeClass("active");
});
// Ensures the DIV displays when your mouse moves away from the main nav menu item
$("#hidden-div").hover(function(){
if (hide) clearTimeout(hide);
$("#menu1").addClass("active");
}, function() {
// If your mouse moves out of the displayed hidden DIV, the DIv fades out and removes the 'active' class
hide = setTimeout(function() {$("#hidden-div").fadeOut("fast");});
$("#hidden-div").stop().fadeIn();
$("#menu1").removeClass("active");
});
});
Pretty it up with some CSS:
/* DIV MENU DEMO LIST STYLE */
#menu ul {
margin: 0;
padding: 0;
list-style: none;
}
#menu ul li {
display: inline;
}
#menu ul li a {
float: left;
display: block;
font: bold 1.1em arial,verdana,tahoma,sans-serif;
line-height: 50px;
color: #888;
text-decoration: none;
margin: 0;
padding: 0 30px;
}
#menu ul li a:hover, #menu ul li a.active {
color: #aaa;
text-decoration: none;
background: #333;
}
/* HIDDEN DIV PANEL STYLE */
#hidden-div {
position: absolute;
width: 900px;
height: 240px;
margin: -1px 0 0 0;
padding: 30px;
background: #333;
display: none;
z-index: 100;
}
/* HIDDEN DIV PANEL - CONTENT INSIDE */
#hidden-div-left {
float: left;
width: 450px;
height: 300px;
}
#hidden-div-right {
float: right;
width: 450px;
height: 300px;
}
#hidden-div-right ul {
margin: 10px 0 0 0;
padding: 20px;
list-style: none;
background: #191919;
overflow: hidden;
}
#hidden-div-right ul li a {
display: block;
font-size: 1.0em;
line-height: 1.0em;
color: #fff;
text-decoration: none;
margin: 0;
padding: 11px 0;
}
#hidden-div-right ul li a:hover {
color: #aaa;
text-decoration: none;
}
I haven't tried adding rich media to the DIV container and while I wouldn't necessarily do that, it should work fine. The real purpose for this was to render basic XHTML and images but if you come up with something cool or use it on your own site, let me know and I'll be glad to showcase it on my site.
Stay well.













I like it! Thanks for sharing.
Posted: February 2, 2010
Great tutorial, very useful. Is it compatible with IE6?
Posted: February 2, 2010
@Jaybird: Thank you!
@Peter: I ran it through IE6 when I first built it but because I don't personally support IE6 anymore, I didn't test it again when I made the style changes. It did work on the initial pass, though.
Posted: February 2, 2010
I don't know what happened, but in my first comment i did mention that i had tested this in IE6 and it worked fine
Posted: February 2, 2010
Cheers. I wish I could drop IE6 but a large number of users to the network of sites Im currently redesigning still use the browser, grrrr
Posted: February 2, 2010
Fantastic tutorial, I was searching for a tutorial like this. Thank you!
Posted: February 2, 2010
Awesome site! Thank you...I have bookmarked this tutorial as I found it to be very helpful! Thank you!!!
Posted: February 3, 2010
Thanks, Eyad. See how I made the demo page grey? ;)
Posted: February 3, 2010
Details: It still works on IE6. Great job ;) Tnx for sharing
Posted: February 6, 2010
That is just awesome. I'm going to use that on a site i'm doing i think. Thank you very, very much indeed. You.da.man.
Posted: February 6, 2010
Really good one...Thanks for sharing....cheers..
Posted: February 6, 2010
Cool stuff! I would suggest adding a stop() method to prevent animation queue buildup. If you roll over and off the nav item multiple times really fast, then leave it and it will keep fading in on it's own. You can apply a simple fix like this to any of your fadeIn/fadeOut methods:
$("#hidden-div").stop().fadeIn();
By the way, I used to live in Ottawa! Just moved back to Saskatchewan 6 months ago :)
Posted: February 6, 2010
@Jordan: *Face palm* I hadn't thought of the queue buildup with this particular script but you're right, especially if you use more than one instance of the effect in a menu. I've added it to the code gave you credit on the demo page.
Funny you should say that...I was born in Yorkton!
Posted: February 6, 2010
I really like the effect! Nicely done!
Btw: Have you never had the problem to spread the items evenly horizontal?
Posted: February 7, 2010
@Erik: Sometimes and in those cases it's a matter of a little math but in this instance it just worked out.
Posted: February 7, 2010
You mention that earier befioe jDiv script was born you said when you were working old a "drop-down navigation with images" - I'm imagening - hopefully - like Adobe.com drop-down with images - OR for eample when you go to Adobe.com and then in the upper right corner in "Search box" type first letter "a" and it will show you beautifyll nav with an image wery well mesured (at least I think it's very pretty, forgive me if I'm wrong), an example of a dsrop down meny with an Image - Now my question is - "Is that jQuery or Ajax or maybe some crazy combination of something else - like php, lol.I would really like to now how to make that one. Thanks
Posted: February 7, 2010
@Vladimir: Adobe likely uses AJAX for their search tool. You can check out Ajax Rain and look for a similar script. I'm sure they have something you could adapt for your project.
Posted: February 7, 2010
You don't need the hidden-div-left (which is terrible naming btw, shouldn't describe style/position), and the megadropdown should be inside the menu list item. You get nothing and lose semantics by putting it somewhere else. Using an h4 that does not follow heading hierarchy doesn't help either. Be wary of creating HTML with layout in mind, it is possible to achieve the same effect with perfectly clean mark-up.
Posted: February 7, 2010
@Ricardo: Yep, I'm aware of that. I named them using 'left' & 'right' that for the purposes of the example so it was quicker to see where it applied. I wouldn't do that in actual practice, of course. As for the H4 tag, that's not necessarily the case. It would depend on the layout of the rest of the page. This demo is for the purposes of understanding how the script works as quickly as possible and I make the assumption that anyone who uses it will know how to format it correctly on their site or as they see fit. That said, I added a comment in the XHTML markup example above. ;)
Posted: February 7, 2010
Thanks for posting this
Posted: February 7, 2010
Nice navigation menu, I like it very much.
But there is something I dont like. Is it possible to build in a kind of delay, that it dont pops up when you hover over the special li element very fast?!
Posted: February 13, 2010
@Andre: Thanks :)
If you want to slow down the speed of the DIV loading, change the jQuery code from .fadeOut("fast"); to .fadeOut("slow");. Easy peasy.
Posted: February 13, 2010
Great stuff!
Now, I roll over and off the nav item multiple times really fast, then hidden div box would never show again. so I deleted the line of
$("#hidden-div").stop().fadeIn();
then it works but the problem that Mr.Boesch mentioned comes again...
sorry of my cheep English
Posted: February 16, 2010
@Skyrocket Labs
I have set jDiv for used with multiple instances but I am looking to remove the fade-in animation entirely and just have the menu function like MTV.com - can you help me out, im not too hot with jQuery yet
Posted: February 18, 2010
@Japanese boy:
Do you have a link to post so I can have a look?
@Peter:
Try this...
$(document).ready(function() {
var hide = false;
$("#menu1").hover(function(){
if (hide) clearTimeout(hide);
$("#hidden-div").show();
$(this).addClass("active");
}, function() {
hide = setTimeout(function() {$("#hidden-div").hide();});
$("#menu1").removeClass("active");
});
$("#hidden-div").hover(function(){
if (hide) clearTimeout(hide);
$("#menu1").addClass("active");
}, function() {
hide = setTimeout(function() {$("#hidden-div").hide();});
$("#menu1").removeClass("active");
});
});
By changing the instances of fadeIn and fadeOut to show and hide, the fading effect is gone. I think I may actually like this version better! Great idea!
Posted: February 20, 2010
@Skyrocket Labs - That works a treat, thanks! I was forgetting to put the '()' after show/hide.
I have uploaded a version with multiple instances and given credit and links to yourself - http://www.peterhintondesign.co.uk/journal/index.php/using-multiple-instances-of-jdiv/. If you want anything on this changing please let me know.
Posted: February 22, 2010
Thanks, Peter! Great job! I posted a link to your version on this page.
Posted: February 22, 2010
Thanks for the code, the kind words and the link-back - much appreciated :o)
Posted: February 23, 2010