-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrollLoader.js
61 lines (50 loc) · 1.5 KB
/
scrollLoader.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
(function(global){
var scrollLoader = function(opt){
var opt = opt || {};
this.opt = {
wrap: opt.wrap || global,
threshold: opt.threshold || 200,
autoLoad: opt.autoLoad || false,
loadUpFn: opt.loadUpFn || null,
loadDownFn: opt.loadDownFn || null
}
this.init();
}
var _prototype = scrollLoader.prototype;
_prototype.init = function(){
this.loading = false;
this.contentBox = this.opt.wrap === global ? document.body : this.opt.wrap;
this.resizeHeight();
this.events();
}
_prototype.resizeHeight = function(){
this.wrapHeight = this.opt.wrap === global ? document.documentElement.clientHeight : this.opt.wrap.clientHeight;
this.conntentHeight = this.contentBox.scrollHeight;
}
_prototype.autoLoad = function(){
this.handler(this);
}
_prototype.handler = function(that){
if(that.loading){
return;
}
var scrollTop = that.contentBox == document.body ? (that.contentBox.scrollTop || document.documentElement.scrollTop) : that.contentBox.scrollTop;
if(that.opt.loadUpFn && scrollTop <= that.opt.threshold){
that.loading = true;
that.opt.loadUpFn(that);
} else if(that.opt.loadDownFn && that.conntentHeight - scrollTop - that.wrapHeight <= that.opt.threshold){
that.loading = true;
that.opt.loadDownFn(that)
}
}
_prototype.events = function(){
var that = this;
if(this.opt.autoLoad){
this.autoLoad();
}
this.opt.wrap.addEventListener('scroll', function(){
that.handler(that)
}, false);
}
global.ScrollLoader = scrollLoader;
})(this || window);