我在使用 Paul Irish 的无限滚动插件时遇到了一些麻烦。如果我只有一页结果,它工作得很好,但如果我有超过一页的结果,它会一遍又一遍地重复最后一页。这是我的无限滚动配置:
$('#grid').infinitescroll({
navSelector : "#paginationControl:first",
nextSelector : "#paginationControl a#next:first",
itemSelector : "#grid .entry",
debug : true,
bufferPx : 500
},
function(newElements){
//Apply masonry
var $newElems = $( newElements );
$newElems.imagesLoaded( function(){
$("#grid").masonry( 'appended', $newElems );
});
//See if this is the last page
if( curPage >= pages ){
alert("Infinite scroll is paused.");
$('#grid').infinitescroll('pause');
}
});
这是我的分页控件:
<?php if ($this->pageCount): ?>
<div class="paginationControl" id="paginationControl">
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
<a href="<?php echo $this->url(array('page' => $this->previous)); ?>" id="previous">
< Previous
</a> |
<?php else: ?>
<span class="disabled">< Previous</span> |
<?php endif; ?>
<!-- Numbered page links -->
<div id="numberedPages">
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<a href="<?php echo $this->url(array('page' => $page)); ?>">
<?php echo $page; ?>
</a> |
<?php else: ?>
<?php echo $page; ?> |
<?php endif; ?>
<?php endforeach; ?>
</div>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
<a id="next" href="<?php echo $this->url(array('page' => $this->next)); ?>" >
Next >
</a>
<?php else: ?>
<span id="next" class="disabled">Next ></span>
<?php endif; ?>
</div>
<?php endif; ?>
我认为由于最后一页上的下一页选择器被禁用,它会在它到达最后一页时停止。为什么它会一遍又一遍地加载最后一页,我该如何阻止它发生?
编辑:
我在回调函数中添加了一个 if 检查以尝试在一段时间后停止滚动,灵感来自这个问题jQuery InfiniteScroll plugin loading the last page over and over。
//See if this is the last page
if( curPage >= pages ){
alert("Infinite scroll is paused.");
$('#grid').infinitescroll('pause');
}
其中 pages 定义为可用的最后一页, curPage 是当前页,每次调用递增。检查在某一点上工作得很好,但我改变了一些东西,我不确定是什么,现在由于某种原因我得到了错误$("#grid").infinitescroll is not a function
$('#grid').infinitescroll('pause');
我不知道无限滚动在那个时候是不是一个功能很明显必须让 javascript 首先得到它。我怎样才能摆脱这个不是函数错误?