2013-01-16 57 views
0

我正在嘗試在我的網站上創建無限滾動功能,但它不起作用。 我的代碼:當用戶滾動到頁面末尾時自動點擊

var post = {} 
post.load_moreBtn = $('#home_load_more'); 
if($(window).scrollTop() + $(window).height() == $(document).height()) { 
    post.load_moreBtn.trigger('click'); 
} 
post.load_moreBtn.on('click', function() { 
    $(this).html('<img src="' + base_url + 'images/core/loader2.gif"/>'); 
    post.load_more_messages($(this).attr('data-last_id')); 
}); 

如果我把警報在地方它的工作原理觸發的,另外,如果我刪除滾動檢測位,負載更多的工作正常。只是不能讓它自動加載,請幫助。

+1

你不綁定任何的滾動事件 –

+0

你的意思是,你不能綁定任何滾動事件? – Saff

+0

你絕對可以,但你發佈的代碼不是 –

回答

1

這很容易使用jQuery:

$(function(){ //on document ready 
    $(document).scroll(function (e) { //bind scroll event 

     var intBottomMargin = 300; //Pixels from bottom when script should trigger 

     //if less than intBottomMargin px from bottom 
     if ($(window).scrollTop() >= $(document).height() - $(window).height() - intBottomMargin) { 
      $("#home_load_more").click(); //trigger click 
     } 

    }); 
}); 

我這個綁定在

+0

謝謝:)我已經開始工作了。但我會採取intbottommargin的想法,這是有道理的,你不必到達頁面的最底部來觸發click事件。謝謝 – Saff

+0

很高興以某種方式幫助=) –

相關問題