nmcDropDown: A Drop-Down Menu Plugin for jQuery
In my post on using template site to save time, I included a quick jQuery plugin for creating drop-down menus. Although that code has served me well I thought it was time to revisit it and make it more flexible and bullet-proof. This is the updated version that I will be including in my blank site template going forward
I found that I was still having to modify my plugin code to accomodate variations in interaction design. Since I sometimes wanted the menus to only appear whe n the y are clicked—rather than on hover—now that is an option. Also, when the top-level menu items are smaller (or when the sub-menus fly out from the side) the usability can be vastly increased by Brian Cherne's wonderful hoverIntent plugin. HoverIntent keeps the sub-menu from disappearing if your pointer leaves the edge for a split-second. This version will use hoverIntent automatically if you have it; otherwise it will revert to the standard hover event. I recommend copying the minified version of hoverIntent into the bottom of your main JavaScript file. It is quite small and including it in your main file will save an http request and speed up your page loading time.
Usage
You should create your navigation as a nested unordered list. Linking the top-level items is optional and will not change the working of the plugin. You should style your menu first with css. How you do that is up to you, but you should set the sub-menus to display: none (this is actually optional, but will prevent the sub-menus from flashing on page-load).
<ul id="nav">
<li>About
<ul>
<li><a href="#">Profile</a></li>
<li><a href="#">Board of Directors</a></li>
<li><a href="#">Contact Info</a></li>
</ul>
</li>
<li><a href="#">Products</a>
<ul>
<li><a href="#">Widgets</a></li>
<li><a href="#">Doohickeys</a></li>
<li><a href="#">Thing-a-ma-bobs</a></li>
</ul>
</li>
</ul>
The nmcDropDown plugin comes with sensible defaults, so you can just call it on the <ul> element that contains your navigation. The plugin will check each menu item to see if it has a sub-menu and set it up as a drop-down if it does.
$('#nav').nmcDropDown();
Configuration
If you want more control over the way your menu works, there are a number of configuration options available to you. Pass an object containing any that you want to change in as the first parameter of the nmcDropDown() call.
$('#nav').nmcDropDown({trigger: 'click'});
Available Options:
- trigger: Event on which to show or hide the sub-menu, can be 'hover' or 'click'. ('hover')
- active_class: Class to give open menu items, useful for styling. ('open')
- submenu_selector: The element immediately below the top-level list-items containing the sub-menu. ('ul')
- show: Effect(s) to use when showing the sub-menu. ({opacity: 'show'})
- show_speed: Speed of the show transition. (300)
- show_delay: Delay before the sub-menu is shown (requires HoverIntent). (50)
- hide: Effect(s) to use when hiding the sub-menu. ({opacity: 'hide'})
- hide_speed: Speed of the hide transition. (200)
- hide_delay: Delay before the sub-menu is hidden (requires HoverIntent). (50)
- fix_IE: This will attempt to fix IE 6 and 7's problems with z-index, where the sub-menu appears behind the body of the page. Set to
falseif it is interfering with your other styling. (true)
Example
We recently launched a website for North Carolina Business Opportunity and Workforce Development, a program that helps women, minorities, and small businesses secure NCDOT contracts. I created drop-down menus that use the nmcDropDown plugin with custom show and hide animations for an interesting effect. The show animation is {opacity: 'show', top: '-=10px'} (hide is exactly the reverse), which fades the menu in while sliding it up from below.
Update: I have also created and written up an example of how to use nmcDropDown, including HTML and CSS.


Comments
Mark Fulton
This is one of the best looking and smooth functioning drop down navigations I've seen, and I've searched alot! Thank you very much for this article and code.
I wasn't aware that multiple JavaScript file calls would have a significant effect on load time, makes sense.. thanks for that tip as well.
joseph
it's really wonderful jQuery menu resources, i like it, stunning!
orbitdesign
Hello,
Thank you vary much
cool :)
Lenny Terenzi
This may be a lifesaver for me! Is there any way to have multi-level drop downs out of this? I am far from a developer but I know enough to get around.
As an aside for anyone else who reads this NMC does GREAT work. I have used them on many project and every one has gone very smooth!
Eli Van Zoeren
Lenny: I will definitely keep multiple levels in mind for a future version. I tend to avoid more than one level, just out of usability concerns, but I recognize that they are sometimes necessary. I will keep you posted!
p.s. Thanks for the compliment!
Lenny Terenzi
Thanks Eli!
I actually agree and after some thought will probably convince my client to keep it simple and navigate to any subsections from its parent section.
Anyway - great work and thanks for quick reply!
ghprod
another great dropdown menu with jQuery!
great job :D
regards
William Rouse
Is there a full souce demmo available?
Lenny
Eli -
I cannot get this to work in IE7. Are there any IE7 tip I ned to know about or did i just break something :)
William Rouse
Lenny:
Or anyone, where did you get the code to start to try? Could you share?
Thanks
Eli Van Zoeren
William: Not right now. I will work on putting one together once I have some time.
Lenny: It should just depend on you css. What kind of problems are you having?
William: You can probably get most of what you need from the linked example. Otherwise, as I said above, I will create an example to demonstrate the html and css at some point.
magburner
It looks fancy enough, but... There are no links if JavaScript is disabled. How is a guy (with a tinfoil hat,) supposed to navigate the website?
Eli Van Zoeren
William, Lenny, & magburner: Please see my new post at http://www.newmediacampaigns.com/page/easy-menus-with-nmcdropdown for a simple demo of how to use this script in real life. (Including a fall-back method in case js is disabled. So you can keep your tinfoil hat on.)
Lenny
I am sure it is in my CSS somewhere. I just cant get the menu to appear when hovered over. Might have to do with my display settings. Your new post may shed some light on it and I will look there before hitting you up again...
Thanks!
ivo
Hi,
thx for this great plugin. It works all fine in Firefox but I had to replace some code to get it working in internet explorer 8.
I had to change "menu = $(this);" in "var menu = $(this);" on line 26.
greets ivo
Erik Jonsson
Very good plugin, nice and easy!
Ivo: I had the same problem, both in ie7 & ie8. Thank you so much for the solution!
Nemanja Cosovic
Thank you! You helped a lot!
Jonathan Kowalski
same problem tahn ivo, same fix, thanks a lot !
wespai
thx collect it to ajax.wespai.com
film izle
Very good plugin, nice and easy!
Ivo: I had the same problem, both in ie7 & ie8. Thank you so much for the solution!
onur
Thank you! You helped a lot!
ilahiler
Thank you for Write
123doing
Jason Chandler
Joel Sutherland
Jason Chandler
Max Diaz
However, when going back to a page linked from a submenu, that menu remains open (using the .min 103 version). I tried to look at your code 105, but then it didn't work at all. The culprit are two lines that have "$(e || this)" instead of $(this).
Anyway, after changing this, I saw you had already fixed submenus in version 105 (as well as the var menu mentioned above), so caveat emptors of the minified version in downloads...
dick
Cyrille Lancien
I have just a suggestion about internal variable in this function :
// iterate each matched element
return this.each(function() {
...
}
All variable should be declare with the 'var' keyword. This because they don't, they will be assume te be part of the window context, and then will be shared will the 'rest of the world' and might cause trouble. In fact, I see a post witch propose to fix a bug for IE, adding the 'var' keyword on 'menu' variable. You should do the same for all internal variables : menu, submenus, over and out.
Doing this, your code will more secure and quick.
Best regards
banana
Julian
Thanks!
alexey
http://jqplug.appspot.com/plugins/dropdown-menu/demo
Gaurav Mehra
Any help
Thanks in advance
Shadde
And @ivo, thanks for your advice to help me fixing problem on IE8.
Leave a Comment