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>
<ul>
    <li><a href="#">Home</a></li>
    <li>
        <a href="#">Tutorials</a>
        <ul>
            <li><a href="#">Sub Nav Link</a></li>
            <li><a href="#">Sub Nav Link</a></li>
        </ul>
    </li>
    <li>
        <a href="#">Resources</a>
        <ul>
            <li><a href="#">Sub Nav Link</a></li>
            <li><a href="#">Sub Nav Link</a></li>
        </ul>
    </li>
    <li><a href="#">About Us</a></li>
    <li><a href="#">Advertise</a></li>
    <li><a href="#">Submit</a></li>
    <li><a href="#">Contact Us</a></li>
</ul>

Step2. CSS

Next, it’s time to style the navigation wireframe with CSS.

  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. }
  11. ul.topnav li {
  12. float: left;
  13. margin: 0;
  14. padding: 0 15px 0 0;
  15. position: relative; /*–Declare X and Y axis base for sub navigation–*/
  16. }
  17. ul.topnav li a{
  18. padding: 10px 5px;
  19. color: #fff;
  20. display: block;
  21. text-decoration: none;
  22. float: left;
  23. }
  24. ul.topnav li a:hover{
  25. background: url(topnav_hover.gif) no-repeat center top;
  26. }
  27. ul.topnav li span { /*–Drop down trigger styles–*/
  28. width: 17px;
  29. height: 35px;
  30. float: left;
  31. background: url(subnav_btn.gif) no-repeat center top;
  32. }
  33. ul.topnav li span.subhover {background-position: center bottombottom; cursor: pointer;} /*–Hover effect for trigger–*/
  34. ul.topnav li ul.subnav {
  35. list-style: none;
  36. position: absolute; /*–Important – Keeps subnav from affecting main navigation flow–*/
  37. left: 0; top: 35px;
  38. background: #333;
  39. margin: 0; padding: 0;
  40. display: none;
  41. float: left;
  42. width: 170px;
  43. border: 1px solid #111;
  44. }
  45. ul.topnav li ul.subnav li{
  46. margin: 0; padding: 0;
  47. border-top: 1px solid #252525; /*–Create bevel effect–*/
  48. border-bottom: 1px solid #444; /*–Create bevel effect–*/
  49. clear: both;
  50. width: 170px;
  51. }
  52. html ul.topnav li ul.subnav li a {
  53. float: left;
  54. width: 145px;
  55. background: #333 url(dropdown_linkbg.gif) no-repeat 10px center;
  56. padding-left: 20px;
  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. }
ul.topnav {
	list-style: none;
	padding: 0 20px;
	margin: 0;
	float: left;
	width: 920px;
	background: #222;
	font-size: 1.2em;
	background: url(topnav_bg.gif) repeat-x;
}
ul.topnav li {
	float: left;
	margin: 0;
	padding: 0 15px 0 0;
	position: relative; /*--Declare X and Y axis base for sub navigation--*/
}
ul.topnav li a{
	padding: 10px 5px;
	color: #fff;
	display: block;
	text-decoration: none;
	float: left;
}
ul.topnav li a:hover{
	background: url(topnav_hover.gif) no-repeat center top;
}
ul.topnav li span { /*--Drop down trigger styles--*/
	width: 17px;
	height: 35px;
	float: left;
	background: url(subnav_btn.gif) no-repeat center top;
}
ul.topnav li span.subhover {background-position: center bottom; cursor: pointer;} /*--Hover effect for trigger--*/
ul.topnav li ul.subnav {
	list-style: none;
	position: absolute; /*--Important - Keeps subnav from affecting main navigation flow--*/
	left: 0; top: 35px;
	background: #333;
	margin: 0; padding: 0;
	display: none;
	float: left;
	width: 170px;
	border: 1px solid #111;
}
ul.topnav li ul.subnav li{
	margin: 0; padding: 0;
	border-top: 1px solid #252525; /*--Create bevel effect--*/
	border-bottom: 1px solid #444; /*--Create bevel effect--*/
	clear: both;
	width: 170px;
}
html ul.topnav li ul.subnav li a {
	float: left;
	width: 145px;
	background: #333 url(dropdown_linkbg.gif) no-repeat 10px center;
	padding-left: 20px;
}
html ul.topnav li ul.subnav li a:hover { /*--Hover effect for subnav links--*/
	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.

  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. });
$(document).ready(function(){

	$("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)

	$("ul.topnav li span").click(function() { //When trigger is clicked...

		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

		$(this).parent().hover(function() {
		}, function(){
			$(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
		});

		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() {
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});

});

*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

sumber : http://www.noupe.com/tutorial/drop-down-menu-jquery-css.html

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: