Need some help?

I'm usually available for small jobs or problem solving with jQuery or css. Just get in touch.

jQuery dropdown menu plugin with css fallback

Partly as an exercise in writing a jQuery plugin, I’ve created a configurable jQuery dropdown menu. In the absence of javascript, the menu reverts to a standard css dropdown menu. It’s based on my original css dropdown menu here. My thinking was to allow the simple use of a few nice jQuery animations without compromising the experience for anyone without javascript.

View a demo here, which illustrates two menus with different options selected, plus another running on css only.

I’ve tested in FF3, IE8, IE7, IE6, Opera 9, Opera 10, Safari 4, Safari 5 and Chrome, all on Win/XP.

Setup

This menu requires jQuery, so you need to load this and the plugin into in your head, plus the css file:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script type="text/javascript" src="/javascript/jquery.orangemenu.js"></script>
<link rel="stylesheet" href="/css/orangemenu.css"/>

The plugin expects html in the form of an unordered list like this:

<ul class="orangemenu" id="mymenu">
<li><a href="#">Item 1</a></li>
<li ><a href="#">Item 2</a>
    <ul>
    <li><a href="#">Item 2.1</a></li>
    <li><a href="#">Item 2.2</a></li>
    </ul>
</li>
<li ><a href="#">Item 3</a>
    <ul>
    <li><a href="#">Item 3.1</a></li>
    <li><a href="#">Item 3.2</a></li> 
    <li><a href="#">Item 3.3</a></li>
    </ul>
</li>
</ul>

Give the menu any id you want, but make sure it has the orangemenu class otherwise it won’t work. Right, now we’ve got the plugin, some css and some html we’re ready to go.

Configuration

To use the plugin with default settings all you need is this:

<script type="text/javascript">
$(document).ready(function(){
$('#mymenu').orangemenu();
});
</script>

Animation effect options

  • ‘fade’ – submenus fade in and out – this is the default effect
  • ‘slide’ – submenus slide down and up
  • ‘diagonal’ – submenus expand top left to bottom right
  • ‘left’ – submenus slide across from left to right
  • ‘slidefade’ – combines the ‘slide’ and ‘fade’ effects
  • ‘diagonalfade’ – combines the ‘diagonal’ and ‘fade’ effects
  • ‘show’ – submenus appear and disappear – this is identical to the default css behaviour but is included for completeness. It also works in IE6, unlike the basic css.

Animation duration options

  • ‘slow’ – 600ms
  • ‘fast’ – 200ms
  • 1000 – 1000ms – or any numeric duration in milliseconds

Configuration examples

This sets an effect of ‘diagonal’ with 600ms duration.

$(document).ready(function(){
    $('#mymenu').orangemenu({duration: 'slow',effect: 'diagonal'});
});

This sets an effect of ‘slide’ with 1000ms duration.

$(document).ready(function(){
    $('#mymenu').orangemenu({duration: 1000,effect: 'slide'});
});

Configuring multiple menus on a page

You probably won’t want this, but the plugin supports multiple usage on a page with different options on each menu. You just need to ensure that each menu has a separate id:

$(document).ready(function(){
    $('#mymenu').orangemenu({duration: 'slow',effect: 'diagonal'});
    $('#mymenu2').orangemenu({duration: 'slow',effect: 'slide'});
});

Download

Orangemenu zip file

Can I use it on my site?

Yes. If you do, just drop me a comment and tell me if it works OK for you (and especially if it doesn’t).

2 responses to “jQuery dropdown menu plugin with css fallback”

  1. Mark says:

    Hi Simon,

    Your nav is awesome, i really like that you can incorporate different effects.

    I do have a question concerning your menu drop down over jquery slides though, it seems to hide behind slides even when i set the z-index to 9999.

    Any advice?

    Thanks,
    Mark

  2. Simon says:

    Hi Mark

    Thanks for your comment. Sounds like a z-index type problem. As the menu dropdowns are positioned absolutely, relative to a relatively positioned container – the parent li – you need to set the z-index on the parent li and make sure that is higher than your jQuery slide. So it needs setting on .orangemenu li, rather than .orangemenu li ul.

Useful? Interesting? Leave me a comment