-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathscrollIt.javascript.js
More file actions
147 lines (119 loc) · 4.36 KB
/
scrollIt.javascript.js
File metadata and controls
147 lines (119 loc) · 4.36 KB
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
if (!NodeList.prototype.forEach) {
var forEach = function (array, callback, scope) {
for (var i = 0; i < array.length; i++) {
callback.call(scope, i, array[i]); // passes back stuff we need
}
};
}
(function(){
'use strict';
// forEach method
var scroll = function(options){
var self = {};
self.options = options;
var animation = null;
var active = 0,
NodeListEl = document.querySelectorAll('[data-scroll-index]'),
lastIndex = NodeListEl[NodeListEl.length-1].dataset.scrollIndex;
var navigate = function(ndx){
if(ndx < 0 || ndx > lastIndex) return;
var targetTop = document.querySelector('[data-scroll-index="'+ ndx + '"]').offsetTop + self.options.topOffset,
distance = targetTop - window.scrollY,
//Define speed to ensure scroll consistancy whether it's scrolling betweem two far away position or two very close position
scrollSpeed = self.options.speed;
//requestAnimationFrame(scrollit);
scrollit();
function scrollit(){
distance = targetTop - window.scrollY;
if (distance > 0){
window.scrollBy(0, scrollSpeed);
if(distance < scrollSpeed ) {
distance = 0;
window.scrollTo(0, targetTop);
return;
}
requestAnimationFrame(scrollit);
}
if (distance < 0){
window.scrollBy(0, -scrollSpeed);
if(distance > - scrollSpeed) {
distance = 0;
window.scrollTo(0, targetTop);
return;
}
requestAnimationFrame(scrollit);
}
if (distance == 0){
return;
}
};
};
self.doScroll = function(e){
var target = e.target.dataset.scrollNav;
navigate(target);
};
function watchActive(){
var winTop = window.pageYOffset;
//padding at top of the highest element
const padding = 16;
function isVisible(node){
return (winTop + padding) >= node.offsetTop + self.options.topOffset &&
(winTop + padding) < node.offsetTop + (self.options.topOffset) + node.offsetHeight;
}
var nodeList = [].slice.call(document.querySelectorAll("[data-scroll-index]")).filter(isVisible);
if(nodeList.length > 0){
var newActive = nodeList[0].dataset.scrollIndex;
updateActive(newActive);
}
};
function updateActive(ndx){
active = ndx;
var navItem = document.querySelectorAll('[data-scroll-nav]');
forEach(navItem, function(key, value){
value.classList.remove(self.options.activeClass);
})
document.querySelector('[data-scroll-nav= "'+ ndx +'"]').classList.add(self.options.activeClass);
};
/**
* keyNavigation
*
* sets up keyboard navigation behavior
*/
//problem
var keyNavigation = function (e) {
var key = e.which;
if(key == self.options.upKey && active > 0) {
navigate(parseInt(active) - 1);
return false;
} else if(key == self.options.downKey && active < lastIndex) {
console.log(lastIndex);
navigate(parseInt(active) + 1);
return false;
}
return true;
};
window.onscroll = function(){
watchActive();
};
window.onkeydown = function(e){
keyNavigation(e);
};
return self;
}
window.scroll = scroll;
})();
var s = scroll({
upKey: 38,
downKey: 40,
scrollTime: 500,
activeClass: 'bg-aqua',
topOffset : 0,
speed: 10,
});
var tar = document.querySelectorAll('[data-scroll-nav]');
forEach(tar, function(key, value){
value.addEventListener("click", function(e){
e.preventDefault();
s.doScroll(e);
})
});