jquery lazy load content in div

后端 未结 3 951
情深已故
情深已故 2021-01-02 19:39

I want to load the content in div.I got many jquery scoll plugins but all are they for whole page load.I want to load content in existing div when scroll reaches at bottom o

3条回答
  •  抹茶落季
    2021-01-02 20:36

    You can do something like this using JQuery and Ajax:

    var loading = false;//to prevent duplicate
    
    function loadNewContent(){       
        $.ajax({
            type:'GET',
            url: url_to_new_content    
            success:function(data){
                if(data != ""){
                    loading = false;
                    $("#div-to-update").html(data);
                }
            }
        });
    }
    
    //scroll DIV's Bottom 
    $('#div-to-update').on('scroll', function() {
        if($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {
            if(!loading){
                loading = true;
                loadNewContent();//call function to load content when scroll reachs DIV bottom                
            }   
        }
    })
    
    
    //scroll to PAGE's bottom
    var winTop = $(window).scrollTop();
    var docHeight = $(document).height();
    var winHeight = $(window).height();
    if  ((winTop / (docHeight - winHeight)) > 0.95) {       
        if(!loading){
            loading = true;
            loadNewContent();//call function to load content when scroll reachs PAGE bottom                
        }       
    }
    

    FIDDLE

提交回复
热议问题