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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog