大家好,我已经做了一个脚本,在滚动发送一些ajax request.After,我会追加响应到div.The的问题是,在滚动点击底部的ajax将发送4-5个请求,而不是一个。我已经设置了一个变量偏移量,它将在每次请求时增加一个偏移量。我需要选择12 items.The偏移量它是为了计算将被跳过的行数(2*12,3*12,等等).The问题是,有时偏移量仍然为0,并且在一些请求之后,偏移量会增加
脚本
$(document).scroll(function(){
if($(window).scrollTop() + $(window).height() > $(document).height() - 500){
string="offset="+offset+'&'+"plusskip="+fistskip;
$('#listaHolder').append('<div class="ajaxLoader"></div>');
$.ajax({
type: "POST",
url: siteURL+"libs/ajax/ajax_foto.php",
data:string,
success:function(response){
offset=offset+1;
fistskip=0;
$('.ajaxLoader').remove();
$('#listaHolder').append(response);
}
});
}
});发布于 2014-06-16 03:50:22
每次滚动它都会调用ajax函数,所以当你超过500的时候,它就会继续调用这个函数。您需要添加一些逻辑来检查ajax是否正在加载,以及是否有更多的内容需要加载。我会将ajax函数和scroll侦听器分开,以使操作更简单。
var loading = false;
$(document).scroll(function(){
if($(window).scrollTop() + $(window).height() > $(document).height() - 500){
if(!loading)
{
ajaxFunction();
}
}
});
function ajaxFunction()
{
loading = true;
$.ajax({
type: "POST",
url: siteURL+"libs/ajax/ajax_foto.php",
data:string,
success:function(response){
offset=offset+1;
fistskip=0;
$('.ajaxLoader').remove();
$('#listaHolder').append(response);
loading = false;
}
});
}我不确定你所说的偏移是什么意思,你能在评论中解释一下吗?
https://stackoverflow.com/questions/24232334
复制相似问题