-
Notifications
You must be signed in to change notification settings - Fork 0
/
scroll.js
executable file
·63 lines (55 loc) · 1.35 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
var scrolling = false;
var scrolldelay;
function pageScroll() {
if (!scrolling) {
return;
}
window.scrollBy(0, 1);
scrolldelay = setTimeout(pageScroll, 20);
}
function stopScroll() {
scrolling = false;
clearTimeout(scrolldelay);
window.scrollBy(0, 0);
}
function startScroll() {
scrolling = true;
pageScroll();
}
function keyboardStopPageScroll() {
scrolling = false;
clearTimeout(scrolldelay);
window.scrollBy(0, 0);
}
function topFunction() {
document.documentElement.scrollTop = 0;
keyboardStopPageScroll();
}
//BACK TO TOP KEYBOARD SHORTCUTS
// on UP⬆︎ key keypress, scroll up to the top of the page
document.addEventListener('keydown', function(event) {
if (event.keyCode == 38) {
topFunction();
keyboardStopPageScroll();
}
});
// on LEFT ⬅︎ key keypress, stop scrolling COMPLETELY ////////
document.addEventListener('keydown', function(event) {
if (event.keyCode == 37) {
stopScroll();
keyboardStopPageScroll();
}
});
// SCROLL DOWN KEYBOARD SHORTCUTS
// on DOWN ⬇︎ key keypress, scroll down or increase scroll speed
document.addEventListener('keydown', function(event) {
if (event.keyCode == 40) {
startScroll();
}
});
// on RIGHT ➡︎ key keypress, scroll down or increase speed
document.addEventListener('keydown', function(event) {
if (event.keyCode == 39) {
startScroll();
}
});