-
Notifications
You must be signed in to change notification settings - Fork 0
/
scroll.js
41 lines (37 loc) · 1.22 KB
/
scroll.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
var smoothScroller = (function() {
var scrollY = 0;
var distance = 30;
var speed = 10;
var __scrollDown = function(id) {
var currentY = window.pageYOffset;
var targetY = document.getElementById(id).offsetTop;
var bodyHeight = document.body.offsetHeight;
var yPos = currentY + window.innerHeight;
var scroller = setTimeout(() => __scrollDown(id), speed);
if (yPos >= bodyHeight) {
clearTimeout(scroller);
} else {
if (currentY < targetY - distance) {
scrollY = currentY + distance;
window.scroll(0, scrollY);
} else {
clearTimeout(scroller);
}
}
}
var __scrollUp = function(id) {
var currentY = window.pageYOffset;
var targetY = document.getElementById(id).offsetTop;
var scroller = setTimeout(() => __scrollUp(id), speed);
if (currentY > targetY) {
scrollY = currentY - distance;
window.scroll(0, scrollY);
} else {
clearTimeout(scroller);
}
}
return {
scrollDown: __scrollDown,
scrollUp: __scrollUp
}
})();