-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrollfixie.js
53 lines (46 loc) · 1.41 KB
/
scrollfixie.js
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
$(document).ready(function() {
$('.nav a').click(function(e) {
e.preventDefault();
var curInx = this.hash;
var target = $(curInx);
var targetOffset = target.offset().top;
$("body").animate({scrollTop: (targetOffset)}, 400, function() {
location.hash = curInx;
});
});
$(document).keydown(function(e){
windowTop = $(window).scrollTop();
e.preventDefault();
if (e.which == (38 || 37)) {
$($(".section").get().reverse()).each(function(i, section, event) {
sectionTop = $(section).offset().top;
if (windowTop>sectionTop) {
$("body").animate({scrollTop: (sectionTop)}, 400);
return false;
}
});
} else if (e.which == (40 || 39 || 13 || 32)) {
$(".section").each(function(i, section){
sectionTop = $(section).offset().top;
if (windowTop<sectionTop) {
$("body").animate({scrollTop: (sectionTop)}, 400);
return false;
}
});
}
});
$(window).scroll(function() {
var sticky = function(index) {
$('li').removeClass('active'), $('.' + index + '-nav').addClass('active');
};
$('.section').each(function(){
var cur = $(this);
var curTop = cur.offset().top;
var curInx = cur.attr("id");
var sidebarTop = $('.'+curInx+'-nav').offset().top - 60;
if (sidebarTop > curTop) {
sticky(curInx);
}
});
});
});