Development Blog

nmcDropDown: A Drop-Down Menu Plugin for jQuery

ncbowd

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 false if 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.

Download

Full, commented version (3.5kb)

Minified version (1.3kb)

Categorized in: ,

About the Author

Eli Van Zoeren

Eli Van Zoeren is a front-end developer and designer at New Media Campaigns. Previously, he received a degree in Visual Communications from Beloit College and had a successful freelance career. You can find out what he's been up to at his personal website or on Twitter.

Thanks for Reading

We appreciate your support. If you liked this article, please share it with others.

Comments

Leave a Comment
  1. Mark Fulton

    Mark Fulton

    October 15, 2009 5:33 PM | Permalink

    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.

  2. joseph

    joseph

    October 16, 2009 9:41 AM | Permalink

    it's really wonderful jQuery menu resources, i like it, stunning!

  3. orbitdesign

    orbitdesign

    October 16, 2009 3:09 PM | Permalink

    Hello,
    Thank you vary much
    cool :)

  4. Lenny Terenzi

    Lenny Terenzi

    October 19, 2009 10:57 AM | Permalink

    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!

  5. Eli Van Zoeren

    Eli Van Zoeren

    October 19, 2009 1:17 PM | Permalink

    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!

  6. Lenny Terenzi

    Lenny Terenzi

    October 19, 2009 2:02 PM | Permalink

    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!

  7. ghprod

    ghprod

    October 20, 2009 9:24 AM | Permalink

    another great dropdown menu with jQuery!

    great job :D

    regards

  8. William Rouse

    William Rouse

    October 23, 2009 2:37 PM | Permalink

    Is there a full souce demmo available?

  9. Lenny

    Lenny

    October 26, 2009 3:15 PM | Permalink

    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 :)

  10. William Rouse

    William Rouse

    October 27, 2009 8:09 PM | Permalink

    Lenny:
    Or anyone, where did you get the code to start to try? Could you share?
    Thanks

  11. Eli Van Zoeren

    Eli Van Zoeren

    October 28, 2009 9:17 AM | Permalink

    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.

  12. magburner

    magburner

    October 30, 2009 5:37 PM | Permalink

    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?

  13. Eli Van Zoeren

    Eli Van Zoeren

    November 02, 2009 11:35 AM | Permalink

    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.)

  14. Lenny

    Lenny

    November 02, 2009 12:29 PM | Permalink

    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!

  15. ivo

    ivo

    November 22, 2009 2:12 PM | Permalink

    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

  16. Erik Jonsson

    Erik Jonsson

    November 26, 2009 2:55 AM | Permalink

    Very good plugin, nice and easy!
    Ivo: I had the same problem, both in ie7 & ie8. Thank you so much for the solution!

  17. Nemanja Cosovic

    Nemanja Cosovic

    December 06, 2009 5:28 PM | Permalink

    Thank you! You helped a lot!

  18. Jonathan Kowalski

    Jonathan Kowalski

    December 07, 2009 4:47 AM | Permalink

    same problem tahn ivo, same fix, thanks a lot !

  19. wespai

    wespai

    December 25, 2009 4:25 AM | Permalink

    thx collect it to ajax.wespai.com

  20. film izle

    film izle

    January 19, 2010 2:04 PM | Permalink

    Very good plugin, nice and easy!
    Ivo: I had the same problem, both in ie7 & ie8. Thank you so much for the solution!

  21. onur

    onur

    January 19, 2010 2:06 PM | Permalink

    Thank you! You helped a lot!

Leave a Comment

URLs will be converted to links. HTML tags will be converted to entities.