无限滚动 jquery 插件
我正在尝试在我使用 Coldfusion 开发的网站上设置无限滚动,我对 javascript 和 jquery 很陌生,所以我在解决所有这些问题时遇到了一些问题。我是否需要在我的网站上设置分页才能使用无限滚动插件,或者有没有办法无需分页?
I am trying to set up infinite-scroll on a site I am developing with Coldfusion, I am new to javascript and jquery so I am having some issues wrapping my head around all of this. Do I need to have pagination on my site in order to use the infinite-scroll plugin, or is there a way to do it with out it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
为此,您不需要无限滚动插件。要检测滚动何时到达页面末尾,您可以使用 jQuery
在 JsFiddle 上执行 Demo
You do not need infinite scroll plug-in for this. To detect when scroll reaches end of page, with jQuery you can do
Demo on JsFiddle
我将 Hussein 的答案与 AJAX 请求一起使用。我修改了代码以在 300 像素而不是 10 像素处触发,但它开始导致我的追加在 AJAX 请求完成之前成倍增加,因为滚动调用在 300 像素范围内触发的频率比在 10 像素范围内触发的频率要高得多。
为了解决这个问题,我添加了一个触发器,该触发器将在 AJAX 加载成功时触发。我的代码看起来更像是这样:
然后在我的 AJAX 响应中,我将
scrollLoad
设置为true
。I'm using Hussein's answer with AJAX requests. I modified the code to trigger at 300px instead of 10px, but it started causing my appends to multiply before the AJAX request was finished since the scroll call triggers much more frequently in a 300px range than a 10px range.
To fix this, I added a trigger that would be flipped on successful AJAX load. My code looks more like this:
then in my AJAX response, I set
scrollLoad
totrue
.我在 Hussein 的小示例之上构建了一个 jQuery 小部件。它支持 localStorage 临时保存附加结果,并且具有暂停功能,可以经常停止附加,需要单击才能继续。
尝试一下:
http://www.hawkee.com/snippet/9445/
I built on top of Hussein's little example here to make a jQuery widget. It supports localStorage to temporarily save appended results and it has pause functionality to stop the appending every so often, requiring a click to continue.
Give it a try:
http://www.hawkee.com/snippet/9445/
有人要求解释,所以这里是解释
$(document).height()--> 是整个文档的高度。大多数情况下,这等于当前文档的元素。
$(window).height()-->是窗口(浏览器)的高度,意味着您在浏览器上看到的任何内容的高度。
$(window).scrollTop()-->Element.scrollTop 属性获取或设置元素内容向上滚动的像素数。元素的scrollTop 是元素顶部到其最顶部可见内容的距离的度量。当元素内容不生成垂直滚动条时,其scrollTop值默认为0。
添加$(window).scrollTop()和$(window).height()现在检查结果是否等于你的文档高度。如果相等意味着您到达了最后。我们也添加了 100,因为我想检查文档底部的 100 像素之前(注意 <= 条件)
如果我错了,请纠正我
Some one asked for explanation so here is the explanation
here $(document).height()-->is the height of the entire document.In most cases, this is equal to the element of the current document.
$(window).height()-->is the height of the window (browser) means height of whatever you are seeing on browser.
$(window).scrollTop()-->The Element.scrollTop property gets or sets the number of pixels that the content of an element is scrolled upward. An element's scrollTop is a measurement of the distance of an element's top to its topmost visible content. When an element content does not generate a vertical scrollbar, then its scrollTop value defaults to 0.
add $(window).scrollTop() with $(window).height() now check whether the result is equal to your documnet height or not. if it is equal means you reached at the end.we are adding 100 too because i want to check before the 100 pixels from the bottom of document(note <= in condition)
please correct me if i am wrong
我遇到了同样的问题,但没有找到适合我需要的插件。所以我写了下面的代码。此代码通过使用 ajax 和分页获取数据将模板附加到元素。
为了检测用户何时滚动到 div 底部,我使用了以下条件:
I had same problem but didn't find suitable plugin for my need. so I wrote following code. this code appends template to element by getting data with ajax and pagination.
for detecting when user scrolls to bottom of div I used this condition:
如果你有一个可滚动的元素,比如一个带有滚动溢出的div,但没有可滚动的文档/页面,你可以采取这种方式。
If you have a scrollable element, like a div with scroll overflow, but no scrollable document/page, you can take this way.
我使用 Hussein 和 Nick 的想法编写了这个函数,但我希望它使用 promises 用于回调。我还希望无限滚动区域位于固定的 div 上,而不仅仅是窗口(如果将 div 发送到选项对象)。我下面的第二个链接中有一个例子。如果你想支持旧版浏览器,我建议使用像 Q 这样的承诺库。 cb 方法可能是也可能不是承诺,但无论如何它都会起作用。
它的使用方式如下:
html
js
如果您希望 feed 暂时停止,您可以在 cb 方法中返回 false。如果您已读完提要,则很有用。可以通过调用 infiniteScroll 的返回对象方法“setShouldLoad”并传入 true 和 example 来与上面的代码一起再次启动它。
无限滚动的功能是这个
1 个以窗口作为滚动条的 feed 示例
2 个 feed 示例,其中 feed 作为滚动条
I wrote this function using Hussein and Nick's ideas, but I wanted it to use promises for the callback. I also wanted the infinite scrolling area to be on a fixed div and not just the window if the div is sent into the options object. There is an example of that in my second link below. I suggest using a promise library like Q if you want to support older browsers. The cb method may or may not be a promise and it will work regardless.
It is used like so:
html
js
If you want the feed to temporarily stop you can return false in the cb method. Useful if you have hit the end of the feed. It can be be started again by calling the infiniteScroll's returned object method 'setShouldLoad' and passing in true and example to go along with the above code.
The function for infinite scrolling is this
1 feed example with window as scroller
2 feed example with feed as scroller