jquery无限滚动“重置”

发布于 2024-12-12 06:15:32 字数 1066 浏览 1 评论 0原文

我正在为网站使用无限滚动 jquery 插件 ( https://github.com/paulirish/infinite -scroll

一切都很好,除了我的页面是一个搜索所以...发生的是:

1)你进入页面,浏览器自动定位你并给你返回一个项目列表(例如酒吧) ) 大约你...需要无限滚动以避免此列表的分页。一切都正常,直到这里......除了我可以到达“无限页面的末尾”并且插件从滚动中“解除绑定”这一事实。

2)现在....当您想在输入文本中手动插入地址时,您可以自由地执行...您写下您的地址,然后按回车键...并使用ajax(无页面刷新)...我会寻找纬度/经度,找到地址,更改无限滚动的导航链接...并且,我感觉很愚蠢,但我无法找到一种方法“重新激活”或“重新绑定”插件到事件......所以我的“新搜索结果”没有新的“无限滚动”实例...

(页面“拆分”正确并正确返回尝试更改“page=NUMBER”的json)

这就是控制台中发生的情况:

["math:", 0, 468]
jquery.infinitescroll.min.js:20["heading into ajax", 
Array[2]
    0 : "/ajax/getCoworkings/?page="
    1 : "&latitude=52.5234051&longitude=13.4113999&distance=12"
    length : 2
    __proto__ : Array[0]
]
jquery.infinitescroll.min.js:20["Using JSON via $.ajax() method"]
jquery.infinitescroll.min.js:20["Error", "end"]
jquery.infinitescroll.min.js:20["Binding", "unbind"]

在“取消绑定”之后,我无法再次绑定它,因此在我的下一个搜索结果上无限滚动........

I'm using the infinite scroll jquery plugin for a website ( https://github.com/paulirish/infinite-scroll )

Everything is fine except that my page is a search so...what happen is:

1) you go on the page, browser auto-locates you and give you back a list of items (eg. bars) around you...Infinite scroll is needed to avoid pagination for this list. Everything works until here...except the fact that i could reache the "end-of-the-infinite-page" and the plugin "unbinds" itself from the scroll.

2) Now....when you want to manually insert an address in the input text, you are free to do it...you write your address, and press enter...and with ajax (no page refresh)...i'll look for lat/lon, locate the address, change the navigation link for the infinite scroll....and,i feel dumb, BUT i can't figure out a way to "reactivate" or "re-bind" the plugin to the event....So my "new search results" do not have a fresh "infinite scroll" instance...

(page "split" correctly and correctly returns a json trying changing "page=NUMBER")

This is what happens in the console:

["math:", 0, 468]
jquery.infinitescroll.min.js:20["heading into ajax", 
Array[2]
    0 : "/ajax/getCoworkings/?page="
    1 : "&latitude=52.5234051&longitude=13.4113999&distance=12"
    length : 2
    __proto__ : Array[0]
]
jquery.infinitescroll.min.js:20["Using JSON via $.ajax() method"]
jquery.infinitescroll.min.js:20["Error", "end"]
jquery.infinitescroll.min.js:20["Binding", "unbind"]

After the "unbind" i'm not able to bind it again and therefore have the infinit scroll on my next search results........

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

泼猴你往哪里跑 2024-12-19 06:15:32

infinitescroll 通过 $.data 将其所有实例数据存储在元素本身上,因此删除该实例数据是完全重置 infinitescroll 的唯一可靠方法。

这两行应该能够彻底摧毁无限滚动。

$elem.infinitescroll('destroy');
$elem.data('infinitescroll', null);

// 现在无限滚动可以正常重新初始化,而不会因前一个实例而出现任何冲突或问题。

请注意,截至 2012 年 6 月,以前的答案都不适用于最新版本的 jquery.infinitescroll (v2.0)

infinitescroll stores all of its instance data on the element itself via $.data, so removing that instance data is the only sure-fire way to totally reset infinitescroll.

These two lines should do the trick of cleanly destroying infinitescroll.

$elem.infinitescroll('destroy');
$elem.data('infinitescroll', null);

// now infinite scroll may be reinitialized normally without any conflicts or cruft lying around from the previous instance.

NOTE that as of June 2012, none of the previous answers worked for me with the latest version of jquery.infinitescroll (v2.0)

迟到的我 2024-12-19 06:15:32

解决了。

我已经添加:

if (xhr == 'destroy') {
    $.removeData(this.element[0]);
}

函数中。

_error: function infscr_error(xhr) {

在第 228 行的

这可能不是最好的方法,但这正是我所需要的。

我现在所做的基本上就是您的建议:

1.

$("#myelement").infinitescroll("destroy");

2.

$("#myelement").infinitescroll(WHATEVER_SETTINGS);

...无需修改“pathParse”值
但那是因为我正在修改选择器(下一个/导航)
之前,使用 jQuery。

Solved.

i've added:

if (xhr == 'destroy') {
    $.removeData(this.element[0]);
}

in function

_error: function infscr_error(xhr) {

on line 228.

Probably that's not the best way of doing this, but it's exactly what i needed.

And what i do -now- is basically what you suggested:

1.

$("#myelement").infinitescroll("destroy");

2.

$("#myelement").infinitescroll(WHATEVER_SETTINGS);

...without the need to modify the "pathParse" values
but that's because i'm modifying the selectors (next/nav)
before, with jQuery.

随风而去 2024-12-19 06:15:32

这里没有提到,但是(无论如何在我的 Infinite Scroll 版本中)您还必须设置两个变量才能重新实例化 InfiniteScroll 才能工作。您还必须重新绑定它。这是代码:

$('#cardContainer').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');

It isn't mentioned here, but (in my version of Infinite Scroll anyway) you also have to set two variables for the re-instantiaion of InfiniteScroll to work. You also have to Rebind it. Here is the code:

$('#cardContainer').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');
逆流 2024-12-19 06:15:32

在尝试其他解决方案无济于事后,这对我有用:

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

参考文档的 1/4: http://www.infinite-scroll.com/infinite-scroll-jquery-plugin/

After trying the other solutions to no avail, this worked for me:

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

Reference 1/4 down the docs: http://www.infinite-scroll.com/infinite-scroll-jquery-plugin/

忆离笙 2024-12-19 06:15:32

我必须做类似的事情,因为我的代码根据用户操作更新目标网址,而不仅仅是依赖计数器。

我最终实现了以下重置方法,然后可以使用 $container('reset'); 调用该方法

reset: function infrscr_reset() {
        this.options.state.isDone = false;
        this.options.state.isDuringAjax = false; // I needed this, others may not
        this._resetmsg();
        this._binding('bind');
    },

我还实现了一个私有方法来重置悬停消息,否则它将继续给出“没有更多页面”通知。

_resetmsg: function infscr_resetmsg() {
        var opts = this.options;
        opts.loading.msg = $('<div id="infscr-loading"><img alt="Loading..." src="' + opts.loading.img + '" /><div>' + opts.loading.msgText + '</div></div>');
        this._debug('Reset loading message');
    },

I've had to do something similar since my code updates the destination url depending on user action rather than just relying on a counter.

I ended up implementing the following reset method that I can then call using $container('reset');

reset: function infrscr_reset() {
        this.options.state.isDone = false;
        this.options.state.isDuringAjax = false; // I needed this, others may not
        this._resetmsg();
        this._binding('bind');
    },

I also implemented a private method to reset the hover message since otherwise it would continue to give a "no more pages" notification.

_resetmsg: function infscr_resetmsg() {
        var opts = this.options;
        opts.loading.msg = $('<div id="infscr-loading"><img alt="Loading..." src="' + opts.loading.img + '" /><div>' + opts.loading.msgText + '</div></div>');
        this._debug('Reset loading message');
    },
夜雨飘雪 2024-12-19 06:15:32

假设您创建了无限滚动,例如:

//This will initiate with default values for example purpose
$("#myelement").infinitescroll();

您不能简单地完全销毁实例吗:

$("#myelement").infinitescroll("destroy");
//Reset anything else that may cause the page to blow up here
//And then create a new instance with different path variables:
//Note, obviously you'll be initializing it with custom selectors/settings etc so include those as well
$("#myelement").infinitescroll({pathParse:["/ajax/getCoworkings/?page=","&latitude=52.5234051&longitude=13.4113999&distance=12"]});

本质上,如果您没有定义 pathParse,脚本会尝试自行解决它。如果您这样做,那么它会使用您提供的内容。它不是很优雅,更像是一种黑客行为,但目前无限滚动并不严格支持更改 pathParse 。

Presuming you created infinite scroll like:

//This will initiate with default values for example purpose
$("#myelement").infinitescroll();

Could you not then simply completely destroy the instance:

$("#myelement").infinitescroll("destroy");
//Reset anything else that may cause the page to blow up here
//And then create a new instance with different path variables:
//Note, obviously you'll be initializing it with custom selectors/settings etc so include those as well
$("#myelement").infinitescroll({pathParse:["/ajax/getCoworkings/?page=","&latitude=52.5234051&longitude=13.4113999&distance=12"]});

Essentially if you don't define pathParse, the script tries to work it out itself. If you do then it uses what you provide. Its not very elegent, more of a hack than anything but changing pathParse isn't strictly supported in infinite-scroll at the moment.

原谅我要高飞 2024-12-19 06:15:32

项目 3)

if (xhr == 'destroy') {
    $.removeData(this.element[0]);

}

也不起作用。至少使用最新版本。砖石工抱怨
'无法在初始化之前调用砌体上的方法;尝试调用方法“reload”

$container.css('display','none');
$container.html(htmlOutput).imagesLoaded( function(){
             $container.css('display','block');
             $container.masonry('reload');
});

// I have to do this in order to "reset" the stage
$container.infinitescroll('destroy'); // Destroy

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

// Init.
$container.infinitescroll({
   navSelector  : '#page-nav',    // selector for the paged navigation 
   nextSelector : '#page-nav a',  // selector for the NEXT link (to page 2)
   itemSelector : '.tile',     // selector for all items you'll retrieve
   loading: {
     finishedMsg: '',
     msgText: '',
     img: url + 'img/ajax-loader.gif'
   }
},
// trigger Masonry as a callback
function( newElements ) {
    // hide new items while they are loading
    var $newElems = $( newElements ).css({ opacity: 0 });
    // ensure that images load before adding to masonry layout
    $newElems.imagesLoaded(function(){
        // show elems now they're ready
        $newElems.animate({ opacity: 1 });
        $container.append($newElems).masonry('appended', $newElems, true); 
    });
 }
);

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

The item 3)

if (xhr == 'destroy') {
    $.removeData(this.element[0]);

}

Did not work as well. At least with the latest version. The masonry complains about
'cannot call methods on masonry prior to initialization; attempted to call method 'reload''

$container.css('display','none');
$container.html(htmlOutput).imagesLoaded( function(){
             $container.css('display','block');
             $container.masonry('reload');
});

// I have to do this in order to "reset" the stage
$container.infinitescroll('destroy'); // Destroy

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

// Init.
$container.infinitescroll({
   navSelector  : '#page-nav',    // selector for the paged navigation 
   nextSelector : '#page-nav a',  // selector for the NEXT link (to page 2)
   itemSelector : '.tile',     // selector for all items you'll retrieve
   loading: {
     finishedMsg: '',
     msgText: '',
     img: url + 'img/ajax-loader.gif'
   }
},
// trigger Masonry as a callback
function( newElements ) {
    // hide new items while they are loading
    var $newElems = $( newElements ).css({ opacity: 0 });
    // ensure that images load before adding to masonry layout
    $newElems.imagesLoaded(function(){
        // show elems now they're ready
        $newElems.animate({ opacity: 1 });
        $container.append($newElems).masonry('appended', $newElems, true); 
    });
 }
);

// Re-initialize
$container.infinitescroll('bind');
放赐 2024-12-19 06:15:32

您只需将以下代码添加到无限滚动声明中即可做到这一点。

errorCallback : function() {
    isc.getContainer().infinitescroll('destroy');
    $.removeData(this);
},

注意不在infinitescroll.js 文件中,它应该在您的自定义无限滚动声明中。

you can do this by just adding the following code into your infinite scroll declaration.

errorCallback : function() {
    isc.getContainer().infinitescroll('destroy');
    $.removeData(this);
},

Note not in infinitescroll.js file, it should be in your custom infinite scroll declaration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文