简体   繁体   English

在Ajax中使用Infinite Scroll jQuery插件

[英]Using Infinite Scroll jQuery Plugin with ajax

I'm doing a project similar to Google Reader. 我正在做一个类似于Google Reader的项目。

I'm using Infinite Scroll jQuery Plugin which works exactly as advertised when viewing the contents of the default selected category (in a scrollable div). 我正在使用Infinite Scroll jQuery插件,该插件的功能与查看默认选定类别(在可滚动div中)的内容完全相同。

However when selecting another category (or folder in the case of Google Reader) and the contents of that category is loaded with ajax into the same div container as above (basically exactly like Google Reader) and scrolling down to page 2 the problems arise as it will move to whatever page was previously selected +1 instead of starting from the beginning when a new category is selected. 但是,当选择另一个类别(或Google Reader的文件夹)并将该类别的内容与ajax一起加载到与上述相同的div容器中(基本上完全类似于Google Reader)并向下滚动到第2页时,就会出现问题将会移动到以前选择+1的任何页面,而不是选择新类别时从头开始。

I think basically I need a way to reset the plugin when a new category is selected. 我认为基本上我需要一种在选择新类别时重置插件的方法。 Any help greatly appreciated. 任何帮助非常感谢。

I realize it was a very general question. 我意识到这是一个非常普遍的问题。 I was hoping to find someone who had done something similar. 我希望找到一个做过类似事情的人。

Anyway, this is probably not optimal but I solved it like this. 无论如何,这可能不是最佳选择,但我这样解决了。

I added this function to the plugin that can be called from out side the plugin: 我将此功能添加到了可以从插件外部调用的插件中:

this.resetPlugin = function() {
  $(document).unbind('retrieve.infscr',kickOffAjax);
  props.currPage = 1;
};

I load the plugin like this 我这样加载插件

window.infinitescroll = $('#content').infinitescroll({usual settings});

Than I can reset it when another category is selected: 比选择其他类别时,我可以重设它:

window.infinitescroll.resetPlugin();

Your answer did not work for me. 您的回答对我没有用。 Here's what I did: 这是我做的:

You have to set two variables for the re-instantiaion of InfiniteScroll to work. 您必须设置两个变量才能使InfiniteScroll生效。 You also have to Rebind it. 您还必须重新绑定它。 Here is the code: 这是代码:

$('#myContainer').infinitescroll('destroy'); // Destroy

// Undestroy
$('#myContainer').infinitescroll({                      
    state: {                                              
        isDestroyed: false,
        isDone: false                           
}
});

InitInfiniteScroll(); // This is a wrapper for the standard initialization you need

// Re-initialize
$('#myContainer').infinitescroll('bind');

Only this works for me, may be this solutions are outdated: 仅这对我有效,可能此解决方案已过时:

jquery infinite scroll "reset" jQuery无限滚动“重置”

Just want to contribute with my answer if someone faces a similar problem. 如果有人遇到类似的问题,只想用我的答案做出贡献。

var off = 0;
var infinite = {};


function loadMorePosts(cat, offset){
            //FUNKY AJAX LOAD
}


function initInfinite(){
    infinite = new Waypoint.Infinite({
      element: $('.infinite-container')[0],
      onBeforePageLoad: function() {
        console.log(off);
        var cat = $('.f-categories').val();
        loadMorePosts(cat, off);
        off += 5;
      },
      onAfterPageLoad: function(){
        //console.log($.noop);
      }
    });
}
initInfinite();

$(function() {
    $('.f-categories').change(function(){
        var cat = $(this).val();
        off = 0;
        infinite.destroy();
        $('.grid').html('');
        initInfinite();
        loadMorePosts(cat,off);
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM