infiniteLoad.js
1.84 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
var $ = require('jquery');
function infiniteLoad(options){
var defaults={
index:0,
isload:true,//是否正在加载
isrun:true,//判断是否执行
offset:{
height:new Function(),
width:new Function()
}
};
this.registerEvent = {
before: [],
change: [],
after: []
};
this.options = $.extend(true, {}, defaults, options);
return this;
};
infiniteLoad.prototype.on = function (name, callback) {
var g = this;
var _e = g.registerEvent[name];
if (_e) {
_e.push(callback);
}
return _e;
};
infiniteLoad.prototype.off = function (name, callback) {
var g = this;
var _e = g.registerEvent[name];
var e = [];
$.each(_e, function (name, _callback) {
if (_callback === callback) {
e.push(name);
}
});
$.each(e.reverse(), function (name, _callback) {
_e.splice(_callback, 1);
});
};
infiniteLoad.prototype.exect=function(key,params){
var g=this,p=this.options;
if (g.registerEvent[key]&&g.registerEvent[key].length > 0) {
for (_e in g.registerEvent[key]) {
g.registerEvent[key][_e](params);
}
}
};
infiniteLoad.prototype.init = function() {
var g=this,p=this.options;
function __loadMore(){
if(p.isload&&g.__directionCalculation()){
p.isload=false;
p.index++;
g.exect("after",p);
}
g.exect("change",p);
}
g.exect("before",p);
$(window).scroll(__loadMore);
};
infiniteLoad.prototype.emit=function(){
var g=this,p=this.options;
p.isload=true;
};
infiniteLoad.prototype.__directionCalculation=function(){
var g=this,p=this.options;
if(p.offset.height()>0&&$(window).scrollTop() + $(window).height() >= p.offset.height()){
return true;
}
if(p.offset.width()>0&&$(window).scrollLeft() + $(window).width() >= p.offset.width()){
return true;
}
return false;
}
module.exports =infiniteLoad;