Animated Drop Down Menu with jQuery

preview

View the effect

Drop down menus are a really convient way to fit a large menu into a really small initial space. For a long time people have just used a form element for standard drop downs, but with minimal effort you can create a much slicker effect using jQuery and CSS.


Step 1: The HTML

Before we can do anything, we need to link our CSS file and our jQuery file in the header our of HTML document:

<link href="css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="jsfiles/jquery.js"></script>

These two files will contain our styles and the javascript effect library (duh), but before we can style or animate anything, we need to build the list itself. We are going to use a simple unordered list:

<ul class="menu_body">
  <li><a href="#">About Us</a></li>
  <li><a href="#">Portfolio</a></li>

  <li><a href="#">Clients</a></li>
  <li><a href="#">Blog</a></li>
  <li><a href="#">Support Forums</a></li>

  <li><a href="#">Gallery</a></li>
  <li><a href="#">Contact Us</a></li>
</ul>

What we have here is as simple as it looks. We have an unordered list with the class of "menu_body". Inside we have multiple list items, each containing a navigation link. Next we need to add an image above the list. This image will serve as the list’s heading an all that is visible when the drop down is collapsed. It doesn’t have to be an image, this would work exactly the same with text to launch and collapse the menu, I just wanted something visual. If you want to use mine, you can find it here. With the image added we have our complete HTML:

<img src="images/navigate.png" width="184" height="32" class="menu_head" />

  <ul class="menu_body">
  <li><a href="#">About Us</a></li>
  <li><a href="#">Portfolio</a></li>

  <li><a href="#">Clients</a></li>
  <li><a href="#">Blog</a></li>
  <li><a href="#">Support Forums</a></li>

  <li><a href="#">Gallery</a></li>
  <li><a href="#">Contact Us</a></li>
</ul>

We need to give our image a class name, so we have something to reference by when we start our jQuery. I used the class "menu_head". Here is what we have so far, a completely unstyled list with an image on top:

Unstyled List


Step 2: The CSS

Next we need to give our list some style. First, lets do our top level styling:

body{background:#534741;font-family:Arial, Helvetica, sans-serif; font-size:12px;}
ul, li{margin:0; padding:0; list-style:none;}

Nothing too comples here, just setting a background color, font, and font size. Also, we are telling the list to have no padding, margin, or bullets. Now our list is just a heading and a neat column of links:

Some Style

Now we can style the heading image and each list item. Here is the CSS:

.menu_head{border:1px solid #998675;}
.menu_body {width:184px;border-right:1px solid #998675;border-bottom:1px solid #998675;border-left:1px solid #998675;}
.menu_body li{background:#493e3b;}
.menu_body li a{color:#FFFFFF; text-decoration:none; padding:10px; display:block;}

Here’s what we just did. We added a light tan border around the image "menu_head". On the unordered list "menu_body" we set a width (the same width as our image), and we added a light tan border to every side but the top (there will already be a line there since we have one around our image). We set a background color for each list item and we styled each link. Every link is now white, not underlines, has a nice amount of padding, and is set to a block display (this will make the whole box around the link clickable, not just the text itself).

Here is what our list should look now:

More Style


Step 3: The jQuery

Our first step will be to add the jQuery to tell the list items to alternate their background colors. In the head of your HTML document, add:

<script type="text/javascript">
$(document).ready(function () {$("ul.menu_body li:even").addClass("alt");
});

</script>

This is a basic jQuery function. When the document is ready, the function will add a special class of "alt" to each alternating row of our list. With the new classes applied, we can add a new CSS rule for the class "alt"

.menu_body li.alt{background:#362f2d;}

Now the rows will alternate between lighter and darker shades of brown, like this:

Row colors

Now the the list has the overall look that we want, we can go ahead and completely hide it with CSS. In the CSS rule for "menu_body" add the property "display:none;" like this:

.menu_body {display:none; width:184px;border-right:1px solid #998675;border-bottom:1px solid #998675;border-left:1px solid #998675;}

If you look at the page now, all you should be able to see if the heading image. For now, the menu has seemingly dissappeared. Time to bring it back with jQuery:

<script type="text/javascript">
$(document).ready(function () {
$("ul.menu_body li:even").addClass("alt");

$('img.menu_head').click(function () {

$('ul.menu_body').slideToggle('medium');

});
});
</script>

We added a new function that runs when the image with the class of "menu_head" is clicked. A click is just one of the events jQuery recognizes. You could also have the function run on mouseover, a key press, or numerous other things. When a click event is registered, jQuery will use the effect slideToggle on the unordered list with the class of "menu_body". jQuery has a large list of effects and various effect plugins, there is really no limit on how you can animate the list sliding open and closed. slideToggle allows you to set a speed, I chose medium, but you can also use "fast", "slow", or define a number in miliseconds.

The menu should now slide open and closed when you click on the heading image:

Animation

You could stop now and have a pretty decent animated menu, but with jQuery its very easy to add simple hover effects. First, we need to add some to our CSS:

.menu_body li a:hover{padding:15px 10px; font-weight:bold;}

This will do two things. When one of our links is hovered over, the padding on the top and bottom will be expanded to 15px (making each rolled over area taller and allowing the shift) and chaning the font weight to bold.

Next we can add some jQuery animation:

<script type="text/javascript">
$(document).ready(function () {
$("ul.menu_body li:even").addClass("alt");

$('img.menu_head').click(function () {
$('ul.menu_body').slideToggle('medium');
});

$('ul.menu_body li a').mouseover(function () {
$(this).animate({ fontSize: "14px", paddingLeft: "20px" }, 50 );
});


});
</script>

We’ve now added a mouseover function, one that runs anytime you mouseover any list item’s link within our unordered list. The function is set to run on "this" which just means the element will run the function on itself. We are using the jQuery effect animate, which allows for many different parameters, along with a duration of time to run them in. We’ve told the function to change the font size to 14px, change the padding-left to 20px, and to do both things in 50 miliseconds.

At this point, the menu can expand and collapse, and the mouseover effect on the links should be working. Now we just need to tell it to reverse the effect when we mouseoff a link. If we don’t tell it to reset each animation, the links will "stick" in their new font size and position. We, of course, want each link to snap back to its original position. We can easily do this like so:

<script type="text/javascript">

$(document).ready(function () {
$("ul.menu_body li:even").addClass("alt");
$('img.menu_head').click(function () {
$('ul.menu_body').slideToggle('medium');
});

$('ul.menu_body li a').mouseover(function () {
$(this).animate({ fontSize: "14px", paddingLeft: "20px" }, 50 );

});

$('ul.menu_body li a').mouseout.(function () {
$(this).animate({ fontSize: "12px", paddingLeft: "10px" }, 50 );
});
});
</script>

This is our last bit of jQuery. It is a function that runs whenever you mouseout of a list item’s link. It will reset the font to the original size (12px), change the padding-left back (to 10px). It will do both things in 50 miliseconds, the same speed as the first half of the animation.

Your menu should now be fully functional. It should be able to click open and close (in a sliding animation) and each link should animate when touched with the mouse (expanding the height, text size, and left-margin). I purposely tried to keep this menu light on images, but this simple effect is really easy to dress up some. For example, each link’s hover state could have an image background. Each image could be specific to each section for a really polished, intuituve menu and it would all fit into a small rectangle when not in use.

You can see the finished menu here.

Download the source files


Introducing QuikTab, a premium CMS style WordPress theme

QuikTab is a standards-compliant, one-page theme powered by WordPress and jQuery. You can buy it at ThemeForest for $12.

28 Comments

  1. Posted October 10, 2008 at 1:52 am | Permalink

    Really cool design…

    defiantly going to use this somewhere.

    http://www.leafydesignz.co.za

  2. Posted October 27, 2008 at 5:27 pm | Permalink

    hi clark

    great site you did. now you’re employed, my question is: aside your work do you have some time free for coding or other projects?

    i’m from switzerland, send you greetings and hope to hear from you.

    marc

  3. jane
    Posted October 31, 2008 at 9:00 am | Permalink

    this is great, but how would you use this as a horizontal menu? it breaks the menu items when you click on them, so the entire top level menu items move down below and open up.

  4. Posted November 1, 2008 at 9:39 pm | Permalink

    Thank you for your website :)
    I made on photoshop backgrounds for myspace or youtube and ect..
    my backgrounds:http://tinyurl.com/6r7cav
    all the best and thank you again!

  5. Posted December 18, 2008 at 5:27 am | Permalink

    Very nice menu indeed. It reminds me the top “Envato Networks” menu on Nettuts, PSDtuts and so on.

    I don’t really like the effects as you rollover a link though. I think that could be improved.

    Keep up the good work.

  6. steve
    Posted January 8, 2009 at 8:27 pm | Permalink

    hi, how can add more buttons and drop menu?? I cant :( why just one? please help!!! thanks

  7. Rodney
    Posted January 12, 2009 at 8:05 am | Permalink

    Thanks for the wonderful tut. Deffinately going to use this one. I was wondering is it possible if you could try and explain how this was done in jquerry?

    http://www.filamentgroup.com/lab/
    jquery_ipod_style_drilldown_menu/

  8. Casey
    Posted January 29, 2009 at 8:16 am | Permalink

    This is great! How would you modify it to use mouseover and mouseout to toggle the menu’s visibility?

  9. Posted January 29, 2009 at 8:56 am | Permalink

    i’m having problem viewing this page. pls fix it. screenshot attached as my website

  10. Nick
    Posted February 13, 2009 at 3:06 pm | Permalink

    I wonder are there a code that if person unmouseover the menu. so it forces menu to close it istead of have to click it to close it?? im curious.

  11. awake
    Posted February 21, 2009 at 3:50 pm | Permalink

    i’ve not viewed the code yet, but I loved the demo.

    i was looking for somethign similar to what envato (netut guys) had.

    thanks a bunch

  12. Posted February 23, 2009 at 11:55 am | Permalink

    Hi, i have one “problem”. I cant make this to go over objects, images or flash, it’s moves all the content down. It is posible TO FLOAT OVER OBJECTS?
    Thanks in advance.

  13. Posted February 23, 2009 at 2:28 pm | Permalink

    @Fede – if you wrap the whole thing in a div and set the div to float (left or right) then when it drops down it will float over your other content.

    The issue I’m having is I can’t get the javascript functions to work. Not even applying the “alt” class works. I just can’t seem to figure out what’s wrong. I did change the class names of my image and unordered list, but I made sure to change the class names in my javascript as well. Any ideas?

  14. Posted February 24, 2009 at 9:59 am | Permalink

    Ok. I finally got it to work. So I have a few constructive criticisms to offer those in the future.

    First, make sure you use double quotes when referencing your class names in the javascript. If you use single quotes the effect will not work.

    Also, make sure you use “normal” instead of “medium” for the slideToggle speed.

    You can learn more about slideToggle over at jQuery’s website:
    http://docs.jquery.com/Effects/slideToggle

  15. daredanger
    Posted March 3, 2009 at 1:35 am | Permalink

    Cannot find navigate.png file in the link that you have given in your post. You have given this link http://www.clarklab.net/blog/posts/animated-drop-down-menu-with-jquery/images/navigate.png. When I visited this page I got Not Found error page. Please update us with another working link or make this link work.

  16. Binno
    Posted March 3, 2009 at 2:01 am | Permalink

    How can i correct program mouseover and mouseout?

    $(’img.menu_head’).mouseover(function () {
    $(’ul.menu_body’).slideToggle(’medium’);
    });

    $(’img.menu_head’).mouseout(function () {
    $(’ul.menu_body’).slideToggle(’medium’);
    });

    When mouseout img.menu.head with mouse to scroll down, then must show on ul.menu.body so long as the mouse leave out ul.menu.body.

    Sorry, i write not good english. I come from Germany.

    Thanking you in advance for this problem solution!

  17. carl
    Posted March 10, 2009 at 1:12 am | Permalink

    I have experimented with using this in a website(not online). It works great on the mac and in Firefox on the PC but…in IE7 the menu items/text jumps out in HUGE font sizes for a split-second before returning to to regular size.
    A jQuery thing? or perhaps confict with something else in my stylesheet?
    Any leads would be appreciated.
    Thanks.

  18. Posted March 12, 2009 at 1:25 am | Permalink

    Hi Clark,

    I love your Quick Tab theme and am thinking about purchasing it within the next couple of days. Would it be possible for me to implement the animated drop down on this them? A couple of the tabs would need to have a submenus. Thanks man.

  19. awake
    Posted March 19, 2009 at 11:54 am | Permalink

    I think I like this version, but there another version @ site below

    - http://hv-designs.co.uk/2009/02/17/sliding-jquery-menu/

  20. Posted May 13, 2009 at 1:10 pm | Permalink

    I really like this menu but like some others I wanted it to function on mouseover and out.
    I am a beginner in javascript and jquery but by looking at another tutorial in jquery I have got it to work.

    $(document).ready(function() {
    $(”ul.menu_body li:even”).addClass(”alt”);
    $(’.container’).hover(function() {
    $(’ul.menu_body’).slideToggle(’medium’);
    }, function() {
    $(’ul.menu_body’).slideToggle(’medium’);
    });
    $(’ul.menu_body li a’).mouseover(function () {
    $(this).animate({ fontSize: “14px”, paddingLeft: “20px” }, 50 );
    });
    $(’ul.menu_body li a’).mouseout(function () {
    $(this).animate({ fontSize: “12px”, paddingLeft: “10px” }, 50 );
    });
    });

    Seems to work in IE7, Firefox and Safari. If there are problems with this script please let me know.

  21. Posted May 13, 2009 at 1:13 pm | Permalink

    You will also need to add to the CSS:

    .container {
    width: 184px;
    }

  22. Posted May 19, 2009 at 12:10 pm | Permalink

    Just FYI: Your layout extends beyond the left margin limit using IE7 and IE8. Just thought you’d like to know.
    Screenshot:
    http://www.nwcic.com/clarklab/clarklab-shot.png

    Best of success,
    JL

  23. Posted May 22, 2009 at 12:21 pm | Permalink

    Very nice.

  24. Posted June 11, 2009 at 7:54 pm | Permalink

    Hi Clark,

    Got the menu working a little too well!

    Is there a way to create a vertical menu that toggles only when you click one item?

    You can see where I’m at here:
    http://chelseajewish.org

    I’m guessing either indiv. classes such as menu_body1, menu_body2 but I haven’t had much experience with jQuery and I’m not sure how to tweak this code.

    Thanks!
    Jenn

  25. Dennis G
    Posted June 12, 2009 at 8:46 am | Permalink

    Hi there. I really like this script (http://clarklab.net/blog/articles/dropdown/example.html). The one thing is that I was trying to modify it such that it will open onmouseover and close onmouseout of anywhere on the menu. Did you have a version like this? Let me know – thanks in advance!

    Dennis

  26. Posted June 15, 2009 at 8:02 pm | Permalink

    Hi Again!

    I found the answer via the Jquery forum to the “all submenus opening at once” issue.

    $(’img.menu_head’).click(function()){
    $(this).next(’ul.menu_body’).slideToggle(’medium’);
    });

    Now it works.

  27. Posted June 20, 2009 at 9:02 am | Permalink

    Hi!
    One more thing. The dropdown is not working in Safari and IE (6-8) Has anyone experimented with creating conditional CSS to handle this? Has anyone else had browser compatibility issues?

    Thanks

  28. Franklyn
    Posted June 27, 2009 at 10:24 pm | Permalink

    The preview is blacklisted by google.

39 Trackbacks

  1. [...] Animated Drop Down Menu [...]

  2. [...] released an excellent tutorial on how to create a vertical animated drop down menu using jQuery. One of the most beautiful drop down menus i’ve seen so far. Spread this post [...]

  3. By: Animated Drop Down Menu - Con jQuery y Css on December 16, 2008 at 5:45 pm

    [...] Animated Drop Down Menu with jQuery, un bonito menú desplegable basado en jQuery y Css. Muy práctico para ubicarlo en la sección superior de nuestro header, ya que ocupa muy poco lugar y a su vez lograra llamar la atención de los usuarios. Me recuerda mucho al menú de NETTUTS como a todos los de la red de Envato. [...]

  4. By: Menús animados con javascript jQuery. | Teimagino.com on December 17, 2008 at 12:33 am

    [...] byron on Dic.17, 2008, under Producción multimedia Easy AdSenser by Unreal Menú desplegable animado jQuery, un buen menú animado desplegable desarrollado en hojas de estilo CSS y javascript [...]

  5. By: Animated Drop Down Menu with jQuery and CSS on December 17, 2008 at 4:07 pm

    [...] Animated Drop Down Menu – Learn how to create a great looking drop down menu with a slick effect using jQuery and CSS. Definitely useful and very easy to implement drop-down site navigation. [...]

  6. [...] jQuery ve CSS ile hazırlanmış olan bu Drop Down menü ile ilgili ingilizce açıklamayı burada bulabilirsiniz. (No Ratings [...]

  7. By: Animated Drop Down Menu with jQuery | Exoxfire on December 18, 2008 at 6:43 pm

    [...] Animated Drop Down Menu with jQuery, un bonito menú desplegable basado en jQuery y Css. Muy práctico para ubicarlo en la sección superior de nuestro header, ya que ocupa muy poco lugar y a su vez lograra llamar la atención de los usuarios. Me recuerda mucho al menú de NETTUTS como a todos los de la red de Envato. [...]

  8. [...] Animated Drop Down Menu [...]

  9. [...] Animated Drop Down Menu with jQuery, un bonito menú desplegable basado en jQuery y Css. Muy práctico para ubicarlo en la sección superior de nuestro header, ya que ocupa muy poco lugar y a su vez lograra llamar la atención de los usuarios. Me recuerda mucho al menú de NETTUTS como a todos los de la red de Envato. [...]

  10. [...] Animated Drop Down Menu with jQuery Ausführliches Tutorial zu animierten Dropdown-Menüs. [...]

  11. By: 25 jQuery Tutorials for Improved Navigation Menus on January 26, 2009 at 6:24 pm

    [...] Animated Dropdown Menu with jQuery [...]

  12. [...] Animated Drop Down Menu with jQuery [...]

  13. [...] 26 – Animated Drop Down Menu with jQuery [...]

  14. By: 100 Best JavaScript Resources | Spoonfed Design on February 9, 2009 at 1:11 pm

    [...] Animated Drop Down Menu with jQuery [...]

  15. [...] Animated Drop Down Menu with jQuery es un bonito menú desplegable basado en jQuery y Css.Ocupa muy poco lugar y a la vez lograra llamar la atención de los usuarios. Tiene un gran parecido al menu de NETTUTS como a todos los de la red de Envato. [...]

  16. By: 100个最佳的JavaScript资源(1~20) | 寂静之声 on February 25, 2009 at 5:43 am

    [...] jQuery的动态下拉菜单 Animated Drop Down Menu with jQuery [...]

  17. By: Menu Drop Down en Wordpress | Eliseos.Net on March 4, 2009 at 4:12 pm

    [...] Por lo que me puse a revisar a mis marcadores y recordé un Menú muy ligero y practico que creo ClarkLab. [...]

  18. [...] Animated Drop Down Menu with jQuery [...]

  19. [...] Animated Drop Down Menu with jQuery [...]

  20. [...] Animated Drop Down Menu with jQuery [...]

  21. By: Menu a discesa animato « TagTagWeb on April 13, 2009 at 8:51 am

    [...] Di seguito il link di un tutorial molto interessante su come costruire un menu a discesa animato utilizzando css e jquery: http://www.clarklab.net/blog/posts/animated-drop-down-menu-with-jquery/ [...]

  22. [...] Animated Drop Down Menu with jQuery [...]

  23. [...] Animated Drop Down Menu with jQuery [...]

  24. [...] Animated Drop Down Menu with jQuery [...]

  25. [...] Animated Drop Down Menu with jQuery [...]

  26. [...] Animated Drop Down Menu with jQuery [...]

  27. [...] Animated Drop Down Menu with jQuery [...]

  28. [...] Animated Drop Down Menu with jQuery [...]

  29. [...] 19. Animated Drop Down Menu with jQuery [...]

  30. [...] 19. Animated Drop Down Menu with jQuery [...]

  31. By: jQuery ve CSS Menü | Sanaldev.net on May 28, 2009 at 10:57 pm

    [...] kendinize göre editleyebilirsiniz. Bu menü hakkında daha ayrıntılı bilgilere ulaşmak için buradan ingilizce olan sayfasına bakabilirsiniz. [...]

  32. [...] 19. jQuery下拉动画菜单 [...]

  33. [...] Animated Dropdown Menu with jQuery [...]

  34. [...] Animated Drop Down Menu with jQuery [...]

  35. [...] Animated Drop Down Menu with jQuery [...]

  36. [...] 19. Animated Drop Down Menu with jQuery [...]

  37. [...] 19. Animated Drop Down Menu with jQuery [...]

  38. [...] WHERE : http://www.clarklab.net/blog/posts/animated-drop-down-menu-with-jquery/ [...]

  39. By: 提升网页设计品质的8种布局方案 on June 30, 2009 at 6:38 pm

    [...] Animated Drop Down Menu with jQuery [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*
  • Skills/References
  • From Flickr

  • Subscribe (RSS)
  • Clark lives in Austin, Texas. He is a freelance web designer. In his spare time, he likes sports and being awesome.
  • templamatic
  • Well Designed

    My New Camera (Canon XSi)

    PaStore: Modular Storage

    Ajax Whois 2.0

    Tennessee Summertime

    Discovery Channel Redesign