在div中jquery延迟加载内容

ZOE*_*ULE 4 jquery html5 scroll

我想加载div中的内容.我有很多jquery scoll插件,但它们都是整页加载.我想在滚动到达div底部时加载现有div中的内容.任何人都可以帮助我吗?

Mer*_*lin 5

你可以使用JQuery和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                
    }       
}
Run Code Online (Sandbox Code Playgroud)

小提琴