jQuery InfiniteScroll插件一遍又一遍地加载最后一页 [英] jQuery InfiniteScroll plugin loads the last page over and over

查看:115
本文介绍了jQuery InfiniteScroll插件一遍又一遍地加载最后一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用紧随jQuery InfiniteScroll 插件可以,但是不断地加载最后一页,无法停止.这是我使用的代码:

I'm using the following jQuery InfiniteScroll plugin which actually works but keeps loading the last page over and over, it can't be stopped. Here's the code which I use:

$('#catalog').infinitescroll({

        navSelector  : "div.page-nav:last",            
                       // selector for the paged navigation (it will be hidden)
        nextSelector : "div.page-nav a.navnext:last",    
                       // selector for the NEXT link (to page 2)
        itemSelector : "#catalog div.single",          
                       // selector for all items you'll retrieve

        bufferPx     : 400
}); 

我的分页如下:

<div id="page-nav" class="page-nav"> 
    <div class="top">
        <a id="page-1" class="active">1</a>
        <a id="page-2" href="?page=2">2</a>
        <a id="page-3" href="?page=3">3</a>
        <a id="page-4" href="?page=4">4</a>
        <a id="page-5" href="?page=5">5</a>
        <a id="page-6" href="?page=6">6</a>
        <a id="page-7" href="?page=7">7</a>
        </div>
    <div class="bottom">
        <span class="right">
            <a class="navnext" href="?page=2" id="next-2">Next</a>
                <span class="next">Ctrl</span>
        </span>
    </div>
</div>

有人可以帮我吗?

谢谢.

推荐答案

确定.这实际上并不是Infinite Scroll插件中的错误.这种情况是,如果没有可用的页面,我的脚本不会返回404错误.它只是简单地返回了被重复添加到内容上的最后一页.为了解决此问题,我存储了我想要显示的所有页面的数量,在每次加载内容后增加了一个变量,并在成功加载所有页面后取消了滚动的绑定.代码如下:

OK. That was not actually a bug in the Infinite Scroll plugin. The case is that my script did not return a 404 error if there was not such a page available. It simply returned the last page which was being appended to the content over and over. To solve this issue I've stored the number of all pages I had and wanted to show, incremented a variable after each content load and unbinded the scrolling once all the pages were loaded successfully. The code is below:

var curPage = 1;
var pagesNum = $("div.page-nav").find("a.pag:last").text();   // Number of pages

$('#catalog').infinitescroll({

        navSelector  : "div.page-nav:last",            
                       // selector for the paged navigation (it will be hidden)
        nextSelector : "div.page-nav a.navnext:last",    
                       // selector for the NEXT link (to page 2)
        itemSelector : "#catalog div.line",          
                       // selector for all items you'll retrieve

        bufferPx     : 800

        }, function() {  // Optional callback when new content is successfully loaded

            curPage++;

            if(curPage == pagesNum) {

                $(window).unbind('.infscr');

            } else {}

});

我希望这会对其他人有所帮助.

I hope this will help somebody else.

这篇关于jQuery InfiniteScroll插件一遍又一遍地加载最后一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆