【问题标题】:Infinite-scroll jQuery plugin keeps populating last page无限滚动 jQuery 插件不断填充最后一页
【发布时间】:2012-03-29 22:28:45
【问题描述】:

我遇到的问题是,infinitescroll 插件可以很好地到达最后一个有效分页,然后如果您再次滚动到底部,它会重新拉出最后一个有效内容。

即如果我有 3 个有效的分页内容,

page/1 = 返回第 1 页内容 page/2 = 返回第 2 页内容 page/3 = 返回第 3 页内容 page/4 = 返回第 3 页内容 page/4 = 返回第 3 页内容 page/4 = 返回第 3 页内容 page/4 = 返回第 3 页内容 等等……

如果我输入 page/99,它仍然会返回第 3 页的内容。

从网上看,我需要将后端配置为在尝试调用不存在的分页页面时返回 404。

我有两个问题:

1) 我客户的网站托管在 SquareSpace.com(他们根本不提供 404 或非常多的后端访问权限)

2) 即使我有访问权限,我也不知道如何修复它。

这里是文章的链接,说明这是问题所在:

https://github.com/paulirish/infinite-scroll/issues/49

如果有人能帮助我,我将非常感激!

【问题讨论】:

    标签: jquery jquery-masonry infinite-scroll


    【解决方案1】:

    我通过向infinite-scroll 回调添加一个计数器来解决问题,如下所示:

    var total = $j(".pagination a:last").html();
    var pgCount = 1;
    var numPg = total;
    
        // jQuery InfiniteScroll Plugin.
        container.infinitescroll({
            navSelector  : '.pagination',
            nextSelector : '.pagination a:first',
            itemSelector : '.journal-entry-wrapper',
            animate: true,
            loading: {
                finishedMsg: 'No more content to load.',
                img: 'http://i.imgur.com/6RMhx.gif'
            }
        },
        // Trigger Masonry as a callback.
        function( newElements ) {
            pgCount++;
    
            if(pgCount == numPg) {
                $j(window).unbind('.infscr');
                container.masonry('reload');
                container.append( newElements ).masonry( 'appended', newElements, true );
                $j('#infscr-loading').find('em').text('No more content to load.');
                $j('#infscr-loading').animate({
                    opacity: 1
                }, 200);
                setTimeout(function() {
                    $j('#infscr-loading').animate({
                        opacity: 0
                    }, 300);
                });
            } else {
                loadPosts(newElements);
            }
        }); 
    
    });
    
    function loadPosts(newElements) {
        // Hide new posts while they are still loading.
        var newElems = $j( newElements ).css({ opacity: 0 });
        // Ensure that images load before adding to masonry layout.
        newElems.imagesLoaded(function() {
            // Show new elements now that they're loaded.
            newElems.animate({ opacity: 1 });
            container.masonry( 'appended', newElems, true );
    
            // Animate opaque post covers on hover.
            $j(newElems).hover(function() {
                $j(this).find('.postCover').stop(true, true).fadeOut(200);
            }, function() {
                $j(this).find('.postCover').stop(true, true).fadeIn(200);
            });
        });
    }
    

    【讨论】:

    • 希望这对其他人有所帮助。仅供参考,这是针对使用 SquareSpace 托管的客户。
    猜你喜欢
    • 2011-06-30
    • 2021-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    相关资源
    最近更新 更多