jquery实现导航固定顶部的效果,仿蘑菇街的,感觉还不错,需要的朋友可以参考下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > <html xmlns= "http://www.w3.org/1999/xhtml" > <head> <meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" /> <title>无标题文档</title> <script type= "text/javascript" src= "js/jquery-1.4.2.min.js" ></script> <script type= "text/javascript" > $(document).ready( function (){ var headHeight=$( ".header" ).height()+10; var nav=$( ".nav" ); $(window).scroll( function (){ if ($( this ).scrollTop()>headHeight){ nav.addClass( "navFix" ); } else { nav.removeClass( "navFix" ); } }) }) </script> <style type= "text/css" > *{ margin:0; padding:0;} body{ _background-attachment:fixed; _background-image:url(about:blank);} //实现ie6不支持fixed方法 .header{ width:1000px; height:60px; margin:0 auto; text-align:center;} .nav{ border:1px solid #ccc; border-radius:5px; overflow:hidden; height:30px; width:1000px; margin:10px auto; background:#fff;} .nav li{ float:left; padding:0 10px; height:30px; line-height:30px;} .nav li a{ text-decoration:none; color: #0CF;} .nav li a:hover{ color: #000; text-decoration:underline;} .nav ul{ list-style:none;} .navFix{ position:fixed; left:0; top:0; _position:absolute; top:expression((offsetParent.scrollTop)+0); z-index:2;} //实现ie6不支持fixed方法 .content{ width:1000px; margin:10px auto;} </style> </head> <body> <div class= "header" > <h1>导航固定在页面顶部测试</h1> </div> <div class= "nav" > <ul> <li><a href= "" >首页</a></li> <li><a href= "" >蓝枫前端</a></li> <li><a href= "" >蓝枫前端</a></li> <li><a href= "" >蓝枫前端</a></li> <li><a href= "" >蓝枫前端</a></li> <li><a href= "" >蓝枫前端</a></li> <li><a href= "" >蓝枫前端</a></li> <li><a href= "" >蓝枫前端</a></li> </ul> </div> <div class= "content" > <p>11111</p> <p>11111</p> <p>11111</p><p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> <p>11111</p> </div> </body> </html> |