-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrollEnd.js
More file actions
64 lines (56 loc) · 1.88 KB
/
Copy pathscrollEnd.js
File metadata and controls
64 lines (56 loc) · 1.88 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
/*
* All code Copyright Benjamin Best 2013 unless otherwise referenced. Licensed
* under the MIT License, see the LICENSE file included in this repository for
* the full terms.
*/
ScrollEnd = (function () {
var ScrollEnd;
function getScrollTop() {
// from http://stackoverflow.com/questions/871399/cross-browser-method-for-detecting-the-scrolltop-of-the-browser-window
if (typeof pageYOffset != 'undefined') {
return pageYOffset;
} else {
var B = document.body;
var D = document.documentElement;
D = (D.clientHeight) ? D : B;
return D.scrollTop;
}
};
ScrollEnd = function (options) {
var checkInterval = (options && options.checkInterval) || 100,
scrolling = false,
lastScrollPosition = getScrollTop(),
i = 0,
that = this;
this.subscribers = [];
setInterval(function() {
var scrollPosition = getScrollTop();
if (scrolling && scrollPosition === lastScrollPosition) {
// we have stopped scrolling
scrolling = false;
for (i = 0; i < that.subscribers.length; i++) {
that.subscribers[i](scrollPosition);
}
} else if (!scrolling && scrollPosition !== lastScrollPosition) {
// we have started scrolling
scrolling = true;
}
lastScrollPosition = scrollPosition;
}, checkInterval);
};
// subscribe to the ScrollEnd event
ScrollEnd.prototype.subscribe = function (func) {
this.subscribers.push(func);
};
// unsusbscribe from the ScrollEnd event
ScrollEnd.prototype.unsubscribe = function (func) {
var idx = this.subscribers.indexOf(func);
if (idx >= 0) {
this.subscribers.splice(idx, 1);
}
};
// Aliases for subscribe and unsubscribe
ScrollEnd.prototype.sub = ScrollEnd.prototype.subscribe;
ScrollEnd.prototype.unsub = ScrollEnd.prototype.unsubscribe;
return ScrollEnd;
})();