简体   繁体   English

jQuery / ajax无限滚动事件

[英]jquery/ajax infinite scroll event

I'm trying to make my own infinite scroll using ajax, php, mysql on my web app. 我正在尝试在我的Web应用程序上使用ajax,php,mysql进行自己的无限滚动。 However I can't find a way to load the new content each time the user is a the end of the page. 但是,我找不到每当用户到页面末尾时都加载新内容的方法。

For now it loads all the data as soon as the user hit the end of the page. 现在,它会在用户点击页面末尾时立即加载所有数据。

Is there any way to make my script 'wait until the end of the last page loaded'. 有什么方法可以让我的脚本“等到最后加载的页面结束”。

$( ".places-wrapper ").scroll(function(){

var windowHeight = $( ".places-wrapper" ).height();
var documentHeight = $( ".places-wrapper")[0].scrollHeight;
var scrollFromTop = $( ".places-wrapper" ).scrollTop();
var margin = 70;

if( scrollFromTop >= documentHeight - windowHeight - margin){

loadNextPage();

}

function loadNextPage(){

    var PlaceID = $( "input[name='placeID']:last" ).val();
    var lastHitDate = $( "input[name='hitDate']:last" ).val();

    $.ajax({
        # .append the new stuff
           });

}

});

I guess what you need is a flag to tell your script that a page is loading. 我猜您需要的是一个标志,以告诉您的脚本页面正在加载。 You may try this approach: 您可以尝试以下方法:

// add a variable as flag
var isLoading = false;

if( scrollFromTop >= documentHeight - windowHeight - margin){

     if ( !isLoading )
     {
        isLoading = true;
        loadNextPage();
     }    
}


// inside your ajax's success callback:

.success(function(){
    isLoading = false;

    // add the page contents
})

As you propably guessed you need an external variable that turns the "load further places"-switch on and off. 正如您可能猜到的那样,您需要一个外部变量来打开和关闭“加载更多位置”。

var scrollActiveTimer = false;

$(".places-wrapper").scroll(function(){

    // Don't continue if a previous ajax request is still active
    if(scrollActiveTimer == true){
        return;
    }

    // For an improved performance you should try to minimize the jquery DOM searches    
    var wrapper = $(".places-wrapper");
    var windowHeight = wrapper.height();
    var documentHeight = wrapper[0].scrollHeight;
    var scrollFromTop = wrapper.scrollTop();
    var margin = 70;

    var scrollEndPosition = documentHeight - windowHeight - margin;

    if( scrollFromTop >= scrollEndPosition){
        loadNextPage();
    }

    function loadNextPage(){
        // Disable the loading of further pages for the moment
        scrollActiveTimer = true;

        var PlaceID = $("input[name='placeID']:last").first().val();
        var lastHitDate = $( "input[name='hitDate']:last" ).val();

        // Activate the loading event again
        scrollActiveTimer = false;

         $.ajax({
             // parameters..
         }).success(function(data){
             // copy data into html
             wrapper.append(data);

             // Activate the loading event again
             scrollActiveTimer = false;
         });
    }

});

I made a fiddle with some dummy functions to simulate the ajax request and html generation: http://jsfiddle.net/Macavity/f77xmho7/2/ 我用一些虚拟函数来模拟ajax请求和html生成: http : //jsfiddle.net/Macavity/f77xmho7/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM