网址导航由于HITS算法的作用,所以有很好的效果。本文实例为大家分享了js网页定位导航的具体代码,供大家参考,具体内容如下
一个循环判断当前滚动到的位置,另一个循环遍历导航条判断其id是否与滚动到的位置相同
效果如图:
实现代码:
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
<!DOCTYPE html> < html >
< head lang = "en" >
< meta charset = "UTF-8" >
< title >地狗购物网--网页定位导航效果</ title >
< style type = "text/css" >
*{
margin: 0;
padding: 0;
}
body{
font-size: 12px;
line-height: 1.7;
}
li{
list-style: none;
}
#content{
width: 800px;
margin: 0 auto;
padding: 20px;
}
#content h1{
color: #0088bb;
}
#content .item{
padding: 20px;
margin-bottom: 20px;
border: 1px dotted #0088bb;
}
#content .item h2{
font-size: 12px;
font-weight: bold;
border-bottom: 2px solid #0088bb;
margin-bottom: 10px;
}
#content .item li{
display: inline;
margin-left:10px ;
}
#content .item li a img{
width: 230px;
height: 230px;
border: none;
}
#menu{
position: fixed;
top: 100px;
left: 50%;
margin-left: 400px;
width: 80px;
}
#menu ul li a{
display: block;
margin: 5px 0;
font-size: 14px;
font-weight: bold;
color: #333;
width: 80px;
height: 50px;
line-height: 50px;
text-align: center;
text-decoration: none;
}
#menu ul li a:hover{
color: #fff;
background: #0088bb;
}
#menu ul li .current{
color: #fff;
background: #0088bb;
}
</ style >
< script type = "text/javascript" >
window.onload = function(){
window.onscroll=function(){
var top = document.documentElement.scrollTop || document.body.scrollTop;
var menus = document.getElementById("menu").getElementsByTagName("a");
var items=document.getElementById("content").getElementsByClassName("item");
var currentId="";
for(var i=0;i< items.length ;i++){
var _item = items [i];
var _itemTop = _item.offsetTop;
if(top>_itemTop - 200){
currentId=_item.id;
}else{
break;
}
}
if(currentId!=""){
//给正确的menu下的a元素class赋值
for(var j=0;j< menus.length ;j++){
var _menu = menus [j];
var _href = _menu .href.split("#");//因为只通过href获取的链接为一长串链接,需要通过#分成数组
if(_href[_href.length-1]!=currentId){
_menu.className = "" ;
}else{
_menu.className = " current" ;
}
}
}
}
}
</script>
</ head >
< body >
< div id = "menu" >
< ul >
< li >< a href = "#item1" class = "current" >1F 男装</ a ></ li >
< li >< a href = "#item2" >2F 女装</ a ></ li >
< li >< a href = "#item3" >3F 美妆</ a ></ li >
< li >< a href = "#item4" >4F 数码</ a ></ li >
< li >< a href = "#item5" >5F 母婴</ a ></ li >
</ ul >
</ div >
< div id = "content" >
< h1 >地狗购物网</ h1 >
< div id = "item1" class = "item" >
< h2 >1F 男装</ h2 >
< ul >
< li >< a href = "#" >< img src = "img/1F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/1F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/1F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/1F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/1F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/1F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/1F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/1F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/1F.jpg" alt = "" /></ a ></ li >
</ ul >
</ div >
< div id = "item2" class = "item" >
< h2 >2F 女装</ h2 >
< ul >
< li >< a href = "#" >< img src = "img/2F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/2F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/2F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/2F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/2F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/2F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/2F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/2F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/2F.jpg" alt = "" /></ a ></ li >
</ ul >
</ div >
< div id = "item3" class = "item" >
< h2 >3F 美妆</ h2 >
< ul >
< li >< a href = "#" >< img src = "img/3F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/3F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/3F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/3F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/3F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/3F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/3F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/3F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/3F.jpg" alt = "" /></ a ></ li >
</ ul >
</ div >
< div id = "item4" class = "item" >
< h2 >4F 数码</ h2 >
< ul >
< li >< a href = "#" >< img src = "img/4F.png" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/4F.png" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/4F.png" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/4F.png" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/4F.png" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/4F.png" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/4F.png" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/4F.png" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/4F.png" alt = "" /></ a ></ li >
</ ul >
</ div >
< div id = "item5" class = "item" >
< h2 >5F 母婴</ h2 >
< ul >
< li >< a href = "#" >< img src = "img/5F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/5F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/5F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/5F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/5F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/5F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/5F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/5F.jpg" alt = "" /></ a ></ li >
< li >< a href = "#" >< img src = "img/5F.jpg" alt = "" /></ a ></ li >
</ ul >
</ div >
</ div >
</ body >
</ html >
|
以上就是实用网址导航的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。