Search

Sunday, February 27, 2011

How to create Drop Down menu with jQuery

Studies show that top navigations tend to get the most visual attention when a user first visits a site. Having organized and intuitive navigation is key — and while most drop down menus may look aesthetically pleasing, developing them to degrade gracefully is also essential. In this tutorial I would like to go over how to create a sexy drop down menu that can also degrade gracefully.

View Demo of Sexy Drop Down Menu
Javascript Disabled in Sexy Drop Down Menu w/ jQuery & CSS
Step1. HTML
First create an unordered list for your base top navigation. Then simply nest another unordered list for your sub navigation.
view plaincopy to clipboardprint?
  1. <ul class="topnav"> 
  2.     <li><a href="#">Home</a></li> 
  3.     <li> 
  4.         <a href="#">Tutorials</a> 
  5.         <ul class="subnav"> 
  6.             <li><a href="#">Sub Nav Link</a></li> 
  7.             <li><a href="#">Sub Nav Link</a></li> 
  8.         </ul> 
  9.     </li> 
  10.     <li> 
  11.         <a href="#">Resources</a> 
  12.         <ul class="subnav"> 
  13.             <li><a href="#">Sub Nav Link</a></li> 
  14.             <li><a href="#">Sub Nav Link</a></li> 
  15.         </ul> 
  16.     </li> 
  17.     <li><a href="#">About Us</a></li> 
  18.     <li><a href="#">Advertise</a></li> 
  19.     <li><a href="#">Submit</a></li> 
  20.     <li><a href="#">Contact Us</a></li> 
  21. </ul> 
Step2. CSS
Next, it’s time to style the navigation wireframe with CSS.
view plaincopy to clipboardprint?
  1. ul.topnav { 
  2. list-style: none; 
  3. padding: 0 20px; 
  4. margin: 0; 
  5. float: left; 
  6. width: 920px; 
  7. background: #222; 
  8. font-size: 1.2em; 
  9. background: url(topnav_bg.gif) repeat-x; 
  10. ul.topnav li { 
  11. float: left; 
  12. margin: 0; 
  13. padding: 0 15px 0 0; 
  14. position: relative; /*--Declare X and Y axis base for sub navigation--*/
  15. ul.topnav li a{ 
  16. padding: 10px 5px; 
  17. color: #fff; 
  18. display: block; 
  19. text-decoration: none; 
  20. float: left; 
  21. ul.topnav li a:hover{ 
  22. background: url(topnav_hover.gif) no-repeat center top; 
  23. ul.topnav li span { /*--Drop down trigger styles--*/
  24. width: 17px; 
  25. height: 35px; 
  26. float: left; 
  27. background: url(subnav_btn.gif) no-repeat center top; 
  28. ul.topnav li span.subhover {background-position: center bottombottom; cursor: pointer;} /*--Hover effect for trigger--*/
  29. ul.topnav li ul.subnav { 
  30. list-style: none; 
  31. position: absolute; /*--Important - Keeps subnav from affecting main navigation flow--*/
  32. left: 0; top: 35px; 
  33. background: #333; 
  34. margin: 0; padding: 0; 
  35. display: none; 
  36. float: left; 
  37. width: 170px; 
  38. border: 1px solid #111; 
  39. ul.topnav li ul.subnav li{ 
  40. margin: 0; padding: 0; 
  41. border-top: 1px solid #252525; /*--Create bevel effect--*/
  42. border-bottom: 1px solid #444; /*--Create bevel effect--*/
  43. clear: both; 
  44. width: 170px; 
  45. html ul.topnav li ul.subnav li a { 
  46. float: left; 
  47. width: 145px; 
  48. background: #333 url(dropdown_linkbg.gif) no-repeat 10px center; 
  49. padding-left: 20px; 
  50. html ul.topnav li ul.subnav li a:hover { /*--Hover effect for subnav links--*/
  51. background: #222 url(dropdown_linkbg.gif) no-repeat 10px center; 
Step3. jQuery
For those who are new to jQuery, you can learn about it here.
The following script contains comments explaining which jQuery actions are being performed.
view plaincopy to clipboardprint?
  1. $(document).ready(function(){ 
  2.     $("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)
  3.     $("ul.topnav li span").click(function() { //When trigger is clicked...
  4. //Following events are applied to the subnav itself (moving subnav up and down)
  5.         $(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click
  6.         $(this).parent().hover(function() { 
  7.         }, function(){ 
  8.             $(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
  9.         }); 
  10. //Following events are applied to the trigger (Hover events for the trigger)
  11.         }).hover(function() { 
  12.             $(this).addClass("subhover"); //On hover over, add class "subhover"
  13.         }, function(){  //On Hover Out
  14.             $(this).removeClass("subhover"); //On hover out, remove class "subhover"
  15.     }); 
  16. }); 
*To degrade gracefully, we only show the drop down menu trigger to those who have javascript enabled.
Degrade Gracefully Demo in Sexy Drop Down Menu w/ jQuery & CSS
This is what it looks like when javascript is disabled:
Javascript Disabled in Sexy Drop Down Menu w/ jQuery & CSS
View Demo of Sexy Drop Down Menu
Conclusion
Note: I went ahead and added the rounded corners to the demo (CSS3 – Only supported in Firefox, Safar, & Chrome). If you would like to give it a try, check out this tutorial.
Experiment and customize this to fit your needs! If you have any questions, concerns, suggestions, or comments, please do not hesitate to let me know.

0 comments:

Post a Comment