上拉刷新

var pageNum = 1;
var isScroll = true;
var loading_hint = $('<div class="loading-hint">暂无更多数据...</div>');
$(window).scroll(function(e) {
var scrollTop = $(this).scrollTop();
var scrollHeight = $(document).height();
var windowHeight = $(this).height();
var positionValue = (scrollTop + windowHeight) - scrollHeight;
if (positionValue >= -100) {
//执行ajax,获取数据
if(isScroll){
isScroll = false;
pageNum++;
$.ajax({
url:'请求的后台接口',
type:'GET',
dataType:'JSON',
success:function (res) {
console.log(res);
console.log(pageNum);
if(pageNum > res.last_page){
              //如果是最后一页 提示
$('.vegetables').after(loading_hint.clone());
// $('.loading-hint').fadeIn();
}else{
isScroll = true;
for(var i = 0;i < res.data.length;i++){
                 //克隆一个标签 循环更新数据 然后添加到页面中
var list_clone = $('.vegetables .vegetables-min:first-child').clone();
list_clone.attr('href','/Mobile/Product/productDetail?product_id=' + res.data[i].id);
list_clone.find('.imgcorn img').attr('src',res.data[i].img);
list_clone.find('.imgcorn_font').text(res.data[i].name);
list_clone.find('.moneyandsales .moneyandsales_money').text('¥' + res.data[i].price);
list_clone.find('.moneyandsales .moneyandsales_sales').text('销量' + res.data[i].salesVolume);
$('.vegetables').append(list_clone);
}
              //如果还有数据 移除提示
$('.loading-hint').remove();
}
},
error:function () {
isScroll = true;
}
})
}
}
e.preventDefault();
});

//提示css
.loading-hint {
display: block;
padding: 15px;
text-align: center;
color: #999;
}
posted @ 2019-03-28 17:36  nie_pengcheng  阅读(444)  评论(0编辑  收藏  举报