第1步:HTML文档

首先为你的导航创建一个无序列表,然后简单的写一下另外一个无序列表作为导航子菜单


1. class="topnav">    
2. "#">Home</a></li>    
3.      <li>    
4. "#">Tutorials</a>    
5. class="subnav">    
6. "#">Sub Nav Link</a></li>    
7. "#">Sub Nav Link</a></li>    
8.          </ul>    
9.      </li>    
10.      <li>    
11. "#">Resources</a>    
12. class="subnav">    
13. "#">Sub Nav Link</a></li>    
14. "#">Sub Nav Link</a></li>    
15.          </ul>    
16.      </li>    
17. "#">About Us</a></li>    
18. "#">Advertise</a></li>    
19. "#">Submit</a></li>    
20. "#">Contact Us</a></li>    
21.  </ul>

<ul class="topnav">
    <li><a href="#">Home</a></li>
    <li>
        <a href="#">Tutorials</a>
        <ul class="subnav">
            <li><a href="#">Sub Nav Link</a></li>
            <li><a href="#">Sub Nav Link</a></li>
        </ul>
    </li>
    <li>
        <a href="#">Resources</a>
        <ul class="subnav">
            <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>


第2步: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
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. /*--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 
34. background-position: center bottombottom; cursor: pointer;}  
35. /*--Hover effect for trigger--*/
36.  ul.topnav li ul.subnav {    
37. list-style: none;    
38. position: absolute; 
39. /*--Important - Keeps subnav from affecting main navigation flow--*/
40. left: 0; top: 35px;    
41. background: #333;    
42. margin: 0; padding: 0;    
43. display: none;    
44. float: left;    
45. width: 170px;    
46. border: 1px solid #111;    
47.  }    
48.  ul.topnav li ul.subnav li{    
49. margin: 0; padding: 0;    
50. border-top: 1px solid #252525; /*--Create bevel effect--*/
51. border-bottom: 1px solid #444; /*--Create bevel effect--*/
52. clear: both;    
53. width: 170px;    
54.  }    
55.  html ul.topnav li ul.subnav li a {    
56. float: left;    
57. width: 145px;    
58. background: #333 url(dropdown_linkbg.gif) no-repeat 10px center;    
59. padding-left: 20px;    
60.  }    
61. /*--Hover effect for subnav links--*/
62. background: #222 url(dropdown_linkbg.gif) no-repeat 10px center;    
63.  }


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;
}

第3步:jQuery

下面的脚本包含的内容解释了JQuery将要执行的的动作,你是jQuery新手?看这里.


 

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

});

下拉菜单只是在支持javascript的的情况下才会显示并触发。