1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
// 楼层滚动
$(window).scroll(
function
(){
var
top = $(document).scrollTop();
//定义变量,获取滚动条的高度
var
menu = $(
"#nav"
);
//定义变量,抓取#menu
var
items = $(
".container"
).find(
".item"
);
//定义变量,查找.item
var
curId =
""
;
//定义变量,当前所在的楼层item #id
items.each(
function
(){
var
m = $(
this
);
//定义变量,获取当前类
var
itemsTop = m.offset().top;
//定义变量,获取当前类的top偏移量
if
(top > itemsTop-300){
curId =
"#"
+ m.attr(
"id"
);
}
else
{
return
false
;
}
});
//给相应的楼层设置cur,取消其他楼层的cur
var
curLink = menu.find(
".active"
);
if
( curId && curLink.attr(
"href"
) != curId ){
curLink.removeClass(
"active"
);
menu.find(
"[href="
+ curId +
"]"
).addClass(
"active"
);
}
// console.log(top);
|
本文转自 小旭依然 51CTO博客,原文链接:http://blog.51cto.com/xuyran/1909498