douji1999 2014-06-28 22:04
浏览 60
已采纳

jQuery Infinite Page Scroll

I am trying to accomplish a ajax content load on page scroll and getting stuck at some parts.

Here is my PHP code:

//Fetch Total Record
$objDB = new DB;
$fetchCount = $objDB
   ->setStoredProc( 'petContestFetchImagesCount' )
   ->execStoredProc()
   ->parseXML();
 $fetchCount = $fetchCount->data;

 //Total number of results
 $totalResults = $fetchCount->totalRecords; 

 //Records per page
 $limit = 5; 

 //Total number of pages / calls we will make based on the results
 $pages = ceil($totalResults/$limit);

//What page are we currently on?
$page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
    'options' => array(
        'default'   => 1,
        'min_range' => 1,
    ),
)));

$offset = ($page - 1)  * $limit;

// Some information to display to the user
$start = $offset + 1;
$end = min(($offset + $limit), $total); 

$nextlink = ($page < $pages) ? '<nav id="page_nav"><a href="?page=' . ($page + 1) . '"></a></nav>' : '';

//Fetch Entries
$objDB = new DB;
$submissions = $objDB
   ->setStoredProc( 'petContestFetchImages' )
   ->setParam("offset", $offset )
   ->setParam("rows", $limit )
   ->execStoredProc()
   ->parseXML(); 

From this point, It knows the number of posts it needs, creates the pagination etc.

I now use an infinateScroll plugin and set it up like so:

    var $container = $('[name=posts]')

    $($container).isotope({
    itemSelector: '.item',
    layoutMode: 'masonry',
    masonry: {
        columnWidth: 10

    }
})

    $container.infinitescroll({
    navSelector: '#page_nav',
    nextSelector: '#page_nav a',
    itemSelector: '.item',
    pixelsFromNavToBottom: -Math.round($(window).height() * 0.9),
    bufferPx: Math.round($(window).height() * 0.9),
    loading: {
        msgText: $container.attr("data-loading-message"),
        finishedMsg: $container.attr("data-no-more-translators")
        }
}, function(newElements) {
    $container.isotope('appended', $(newElements))
    })

})

As I scroll down the page, it is loading the content fine. However, it gets to the last record and keeps loading only the last record every time. If I scroll up and then down again, it loads just the last record.

I tested my stored procedure and its not even outputting results when I say start at record 11 (the last result) and give me 5 more, it returns no data.

Not sure what I am missing here but it shouldnt load any more records after hitting the last one.

I used this for reference: http://blog.lingohub.com/developers/2013/09/endless-pages-scrolling-masonry/

EDIT:

This client side change works however it doesnt prevent the ajax call from firing, just from appending any results that it does fine.

var tmp = 1;
var pages = '<?php echo $pages; ?>

$container.infinitescroll({
    navSelector: '#page_nav',
    nextSelector: '#page_nav a',
    itemSelector: '.item',
    pixelsFromNavToBottom: -Math.round($(window).height() * 0.9),
    bufferPx: Math.round($(window).height() * 0.9),
    loading: {
        msgText: $container.attr("data-loading-message"),
        finishedMsg: $container.attr("data-no-more-translators")
        }
}, function(newElements) {
    if(tmp < pages){
        $container.isotope('appended', $(newElements))
    }
    tmp++;
    })

})
  • 写回答

1条回答 默认 最新

  • douyue9704 2014-06-28 22:33
    关注

    I would think you would just do a check to see if you've reached the max page count?

      if($page<$pages){
           //Fetch Entries
           $objDB = new DB;
           $submissions = $objDB
            ->setStoredProc( 'petContestFetchImages' )
            ->setParam("offset", $offset )
            ->setParam("rows", $limit )
            ->execStoredProc()
            ->parseXML(); 
        }
    

    Then change the nextlink to echo one extra link higher than $pages variable. Like so...

        $nextlink = ($page <= $pages) ? '<nav id="page_nav"><a href="?page=' . ($page + 1) . '"></a></nav>' : '';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?