-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoScroll.js
34 lines (31 loc) · 1.02 KB
/
autoScroll.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
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
function autoScroll(selector) {
var scrollAttempts = 0;
var incrementScrollAttempts = debounce(function() {
scrollAttempts++;
});
window.addEventListener('scroll', incrementScrollAttempts);
var el = document.querySelector(selector);
var chkReadyState = setInterval(function() {
if (el) {
window.scrollTo(0, el.offsetTop);
}
if (document.readyState == 'complete' || scrollAttempts > 1) {
clearInterval(chkReadyState);
window.removeEventListener('scroll', incrementScrollAttempts, false);
}
}, 100);
};