2013-10-30 34 views
1

我每次都在窗口的用户scrolls to the bottom附加一些php files using ajax为同一事件的每次发生追加数据

$(window).scroll(function(){ 
    if($(window).scrollTop() + $(window).height() > $(document).height() - 100){ 
    $(window).unbind('scroll'); 
    // Function to append php files using ajax 
    } 
}) 

我想认下一次用户滚动到页面的底部,并追加一些其他PHP文件,但我如何找出滚动到页面底部的下一个(1或多个)事件?

Example: First scroll to bottom: Append 1.php 2.php 
     second scroll to bottom: append 3.php 4.php 
     third scroll to bottom: append 5.php 6.php 
     4th scroll to bottom: show footer 

我不需要infinite scroll plugin。因为那里没有footer的概念。

+0

为什么不简单地算一下......? – Johan

+0

我做了它不正常....我有一个if语句来执行一个块 –

回答

1

您需要维护一个计数器变量,它记录您已经完成了多少个请求。然后,您可以将其传递给服务器,然后服务器将返回所需的信息。事情是这样的:

var requestCount = 0; 
$(window).scroll(function(){ 
    if ($(window).scrollTop() + $(window).height() > $(document).height() - 100) { 
     $(window).unbind('scroll'); 
     // Function to append php files using ajax 
     requestCount++; 
     if (requestCount < 4) { 
      $.ajax({ 
       url: 'foo.php', 
       data: { page: requestCount }, 
       success: function() { 
        // append your items 
       } 
      } 
     }  
     else { 
      // show footer 
     } 
    } 
}) 

在你的PHP,你将需要采取page变量和返回相关的项目。

+0

它不能解决我的问题,我接受它只是为了减少答案 –

0
var count = 1; 
$(window).scroll(function(){ 
    if($(window).scrollTop() + $(window).height() > $(document).height() - 100){ 
    $(window).unbind('scroll'); 
     if(count<4) 
      // Function to append php files using ajax 
      call_your_ajax_with_url(count+'.php'); 
      call_your_ajax_with_url(count+1 +'.php'); 
      count+=1; 
     else showFooter(); 
    } 
}); 

会做这项工作。