2013-09-27 45 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

很高兴帮助你:) –