Senin, 06 September 2010

Amazing Dropdown Boxes Drives Visitors..

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.


Image1 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.
  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.
  1. ul.topnav {  
  2.     list-stylenone;  
  3.     padding: 0 20px;  
  4.     margin: 0;  
  5.     floatleft;  
  6.     width920px;  
  7.     background#222;  
  8.     font-size: 1.2em;  
  9.     backgroundurl(topnav_bg.gif) repeat-x;  
  10. }  
  11. ul.topnav li {  
  12.     floatleft;  
  13.     margin: 0;  
  14.     padding: 0 15px 0 0;  
  15.     positionrelative/*--Declare X and Y axis base for sub navigation--*/  
  16. }  
  17. ul.topnav li a{  
  18.     padding10px 5px;  
  19.     color#fff;  
  20.     displayblock;  
  21.     text-decorationnone;  
  22.     floatleft;  
  23. }  
  24. ul.topnav li a:hover{  
  25.     backgroundurl(topnav_hover.gif) no-repeat center top;  
  26. }  
  27. ul.topnav li span { /*--Drop down trigger styles--*/  
  28.     width17px;  
  29.     height35px;  
  30.     floatleft;  
  31.     backgroundurl(subnav_btn.gif) no-repeat center top;  
  32. }  
  33. ul.topnav li span.subhover {background-positioncenter bottombottomcursorpointer;} /*--Hover effect for trigger--*/  
  34. ul.topnav li ul.subnav {  
  35.     list-stylenone;  
  36.     positionabsolute/*--Important - Keeps subnav from affecting main navigation flow--*/  
  37.     left: 0; top35px;  
  38.     background#333;  
  39.     margin: 0; padding: 0;  
  40.     displaynone;  
  41.     floatleft;  
  42.     width170px;  
  43.     border1px solid #111;  
  44. }  
  45. ul.topnav li ul.subnav li{  
  46.     margin: 0; padding: 0;  
  47.     border-top1px solid #252525/*--Create bevel effect--*/  
  48.     border-bottom1px solid #444/*--Create bevel effect--*/  
  49.     clearboth;  
  50.     width170px;  
  51. }  
  52. html ul.topnav li ul.subnav li a {  
  53.     floatleft;  
  54.     width145px;  
  55.     background#333 url(dropdown_linkbg.gif) no-repeat 10px center;  
  56.     padding-left20px;  
  57. }  
  58. html ul.topnav li ul.subnav li a:hover { /*--Hover effect for subnav links--*/  
  59.     background#222 url(dropdown_linkbg.gif) no-repeat 10px center;  
  60. }  

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.
  1. $(document).ready(function(){  
  2.   
  3.     $("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)  
  4.   
  5.     $("ul.topnav li span").click(function() { //When trigger is clicked...  
  6.   
  7.         //Following events are applied to the subnav itself (moving subnav up and down)  
  8.         $(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click  
  9.   
  10.         $(this).parent().hover(function() {  
  11.         }, function(){  
  12.             $(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up  
  13.         });  
  14.   
  15.         //Following events are applied to the trigger (Hover events for the trigger)  
  16.         }).hover(function() {  
  17.             $(this).addClass("subhover"); //On hover over, add class "subhover"  
  18.         }, function(){  //On Hover Out  
  19.             $(this).removeClass("subhover"); //On hover out, remove class "subhover"  
  20.     });  
  21.   
  22. });  
*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

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.

Tidak ada komentar:

Posting Komentar