2013-09-27 46 views
0

我有一塊JS代碼,我只需要將這些部分連接起來就可以工作。 我的js代碼將加載更多的內容,從數據庫中提取。直到現在它工作得很好,當用戶點擊一個鏈接時,會加載更多。javascript滾動底部時執行函數

現在我想讓它在頁面接近尾聲時自動加載。 我的代碼:

加載更多功能

function myFunction() 
{ 
$(window).scroll(function() { 
    if ($(window).scrollTop() >= $(document).height() - $(window).height() - 10) { 
     $("load_more"); <---- **HOW DO I CALL THE LOAD FUNCTION?** 
    } 
}); 

$(function() { 
$(document).on("click",".load_more",function() { 
var last_msg_id = $(this).attr("id"); 
var device=<?php echo $idevice; ?>; 
if(last_msg_id!='end'){ 
$.ajax({//Make the Ajax Request 
type: "POST", 
url: "index_more.php", 
data: "lastmsg="+ last_msg_id+"&aa="+device, 
beforeSend: function() { 
$('a.load_more').html('<img src="img/loading.gif" />'); 
}, 
success: function(html){ 
    $("#more").remove(); 
$("ol#updates").append(html); 
} 
}); 

} 
return false; 
}); 
}); 

所以,我的問題是我如何調用從$的 「load_more」 功能(窗口).scroll?

謝謝!在按鈕

+0

你不需要包裝'$(window).scroll(function()'裏面的函數... –

回答

2

只需觸發點擊,

$(window).scroll(function() { 
    if ($(window).scrollTop() >= ($(document).height() - $(window).height() - 10)) { 
     $(".load_more").trigger('click'); 
    } 
}); 

所以一些修改也需要,

$(function() { 

    $(window).scroll(function() { 
     if ($(window).scrollTop() >= ($(document).height() - $(window).height() - 10)) { 
      $(".load_more").trigger('click'); 
     } 
    }); 

    $(document).on("click",".load_more",function() { 
     var last_msg_id = $(this).attr("id"); 
     var device=<?php echo $idevice; ?>; 
     if(last_msg_id!='end'){ 
      $.ajax({//Make the Ajax Request 
       type: "POST", 
       url: "index_more.php", 
       data: "lastmsg="+ last_msg_id+"&aa="+device, 
       beforeSend: function() { 
        $('a.load_more').html('<img src="img/loading.gif" />'); 
       }, 
       success: function(html){ 
        $("#more").remove(); 
        $("ol#updates").append(html); 
       } 
      }); 

     } 
     return false; 
    }); 
}); 

希望這符合要求。

+0

完美,它做的工作!謝謝。 – Theodoros80

+0

很高興幫助你:) –