var GOODS = function() {
this.container = $('#goodsList');
this.domMore = $('#goodsMore');
this.pageSize = 10;
this.currentIndex = 0;
this.isAjaxing = false;
this.hasMore = true;
this.h = document.documentElement.clientHeight;
this.init();
};
GOODS.prototype = {
init: function() {
var _self = this;
_self.model();
$(window).on('resize', function() {
_self.winHeight = document.documentElement.clientHeight;
});
$(window).on('scroll', function() {
if (_self.hasMore && !_self.isAjaxing) {
if (_self.h - _self.domMore.get(0).getBoundingClientRect().top > 30) {
_self.model();
}
}
});
},
model: function() {
var _self = this;
_self.currentIndex += 1;
_self.isAjaxing = true;
_self.domMore.text('正在载入数据...');
$.post('url', {
pageIndex: _self.currentIndex,
pageSize: _self.pageSize,
pageId: '1145',
d: (new Date().getTime() + Math.floor(Math.random() * 100))
}, function(data) {
_self.isAjaxing = false;
if ('00' === data.code) {
var list = data.result;
list.length > 0 && _self.view(list);
if (list.length < _self.pageSize) {
_self.hasMore = false;
if (1 == _self.currentIndex && 0 == list.length) {
_self.domMore.text('没有已上架的商品哦~');
} else {
_self.domMore.text('没有更多了哦~');
}
} else {
_self.domMore.text('上拉下载更多...');
}
} else {
console.log(data.message);
}
}, 'json');
},
view: function(list) {
var _self = this;
var tpl = '';
var len = list.length;
for(var i = 0; i < len; i ++) {
var item = list[i];
if (item.stockQty > 0) {
tpl += '';
}
}
_self.container.append(tpl);
}
};
new GOODS();
js分页加载更多
最新推荐文章于 2024-07-01 11:34:43 发布