|
|
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; |
...
|
...
|
|