Read the latest web development and design tips at Fred Wu's new blog! :-)
Poke me on GitHub

Release: [jQuery Plugin] Endless Scroll

Please check the GitHub repository for the latest release.

If you don’t already know, endless scroll (or infinite scrolling) is a popular technique among web 2.0 sites such as Google Reader and Live Image Search, where instead of paging through items using the traditional pagination technique, the page just keeps loading with new items attached to the end.

I have developed a jQuery plugin to easily achieve this.

Requirement: jQuery 1.2+

The plugin is tested with jQuery 1.2.6, 1.3 and 1.4.

There are a few options to customise the behaviour of this plugin:

  • bottomPixels (integer) – the number of pixels from the bottom of the page that triggers the event
  • fireOnce (boolean) – only fire once until the execution of the current event is completed
  • fireDelay (integer) – delay the subsequent firing, in milliseconds. 0 or false to disable delay.
  • loader (string) – HTML loader
  • data (string) – plain HTML data
  • insertAfter (string) – jQuery selector syntax: where to put the loader as well as the plain HTML data
  • callback (function) – callback function, accepets one argument: fire sequence (the number of times the event triggered during the current page session)
  • resetCounter (function) – resets the fire sequence counter if the function returns true, this function could also perform hook actions since it is applied at the start of the event

In a typical scenario, you won’t be using the ‘data’ option, but rather the ‘callback’ option. You may use it to trigger an AJAX call and updates/inserts your page content.

Usage

// using default options
$(document).endlessScroll();
// using some custom options
$(document).endlessScroll({
	fireOnce: false,
	fireDelay: false,
	loader: "<div class="loading"><div>",
	callback: function(p){
		alert("test");
	}
});

Demo

Click here for a simple demo.

Download / Source Code

Changelog

v1.4.2 [2011-01-08]

  • Fixed a bug where calling the script on $(document) would fail.

v1.4.1 [2010-06-18]

  • Fixed a bug where the callback fires when the inner wrap hasn’t been created.

v1.4 [2010-06-18]

  • Endless Scroll now works with any elements, not just $(document)!

v1.3 [2009-04-20]

  • Fixed a bug caused by ‘fireDelay‘.

v1.2 [2009-01-16]

  • Added ‘resetCounter‘ option.

v1.1 [2009-01-15]

  • Added ‘fireDelay‘ option.

v1.0 [2009-01-15]

  • Initial release.
  • Digg
  • DZone
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Twitter

Related posts

Tags: , , , , , , , ,

Comments Section

90 Responses to “Release: [jQuery Plugin] Endless Scroll”

Sidebar might be covered by comments ... consider it a feature! ;)
  1. 1

    Thanks for the plugin.

    Was looking for similar feature.

    Thanks again.

  2. 2

    Sounds good, any chance of a working demo or two? :-)

  3. 3

    Awesome. I did something like this a while back ago. Yours is probably a hell of a lot better. =)

  4. 4

    Demo?

  5. 5

    I’ve now updated the post to include a simple demo link. :)

  6. 7

    Looks great! I’m sure I’ll be able to put this to use. Thanks for the great work!

  7. 8

    cool plugin. I had an attempt to do that. But did not finish that time. But nice to see that.

  8. 9

    I was wondering if this will for for example if i want to have the user jump to a certain row – for example in your Demo, can you give the user the option to load the page from “Added text 260″ – or any number they like?

  9. 10

    @Sami, yes you can use the fireSequence argument in the callback function for that.

  10. 12

    Hi Fred, thanks for the script. Which all browsers have you tested the script with or are you aware of any cross browser issues?

  11. 13

    @Piyush, the script has been tested on Firefox 3, Safari 3/4, IE 7 and Chrome. It should work on all major browsers, if not please let me know.

  12. 14

    uhm..the demo does not seem to work here on Safari and FF.. just normal static text lines..

  13. 15

    [Edit]: ups, sorry, thought this was about a carousel :P works properly then..

  14. 16

    I’m trying to use this with jQuery tabs and have ran into a problem. I try to define which element should trigger endlesscroll, but it seems that only “document” works. For example, I have several tabs and want a different scroll working for each one of them. Any ideas on how to do this?

  15. 17

    Demo link appears to be broken? I’m getting a 404 when i go there.

  16. 18

    it works fine for me

  17. 19

    This plugin is awesome. Thank you for sharing!

    How can I keep this from being fired multiple times right in a row? I thought fireDelay set to say, one or two seconds (1000 / 2000 ms) would prevent this but it seems to ignore the fireDelay integer all together. There are times when it will fire back to back very quickly with no delay.

  18. 20

    Try increase the fireDelay time, if you’re still having this problem, please post your OS and browser versions.

  19. 21

    Thanks for such a speedy reply. I changed the number

    I’m running Mac OS X 10.5.8 running Firefox 3.5.2.

    For what it’s worth, here’s my code: http://pastebin.com/f119a26d9

    I’ve tried the integer using quotes and no quotes. I’m fetching a set of records and setting the start number based on the amount of articles present. To begin with there are 15 articles, so the first scroll will return the start number of 15. But, if you scroll aggressively it’s possible to fire off two of those calls consecutively.

  20. 22

    How can i apply this to a table?

  21. 25

    Can I modify this to run the scroll horizontally instead of vertically? I’m new to jQuery, so apologize if the answer shoudl be self-evident.

  22. 26

    I haven’t tested horizontal scrolling, but I think with proper modification, it should run fine.

  23. 27

    Yeah, Fred, see that’s my problem: I’m not sure what or how to modify it. I’m not sure that simply changing “bottom” and ‘Top’ references to ‘left’ and ‘right’ references will work. Any ideas?

  24. 28

    Hi,

    Could you explain to me what the P variable in the function does? I am trying to get the content loaded by AJAX but I am not sure how to keep track of what content that has been loaded?

  25. 29

    @Joakim:

    Callback function, accepts one argument: fire sequence (the number of times the event triggered during the current page session)

  26. 30

    Can you give an example of how to use ceaseFire? How do I turn off scrolling once the callback function returns no new entries? Thanks. Nice plugin.

  27. 32

    It’s not endless if you grab scroll bar and slide it to the end.

  28. 33

    I had success in modifying the plugin to work with a given scrollable container and a child content element instead of window and document. The default object sets these to window and document but other elements can be set as options when calling endlessscroll().

    I was a bit surprised the plugin didn’t already do this. Any reason why not?

  29. 34

    @Peter: The repository for the Endless Scroll is on Github, would you be able to submit a patch or fork the project and send a pull request?

  30. 36

    with jquery 1.4.2
    Node cannot be inserted at the specified point in the hierarchy” code: “3
    any idea?

  31. 37

    Does not works with jQuery 1.4.2!

  32. 38

    Hi,

    Great plugin, but like other people I can’t get it to work with 1.4.2.

    Will there ever be an update for this script or is it better to look for another one?

    Thanks in advance.

  33. 39

    Just a quick update – yes I will be updating the script to work under jQuery 1.4. It’s the holiday seasons and all so it might take a bit longer.

  34. 40

    I’ve released a new version that should fix the issue, please download it from the Github repo: https://github.com/fredwu/jquery-endless-scroll/downloads.

  35. 41

    Hey, really appreciate the work you did on this plugin. Cheers!

  36. 42

    Hi,

    I currently use page pagination on my website and love what you have created and offer to share with everyone.

    How easy if this to implement when results are taken from a mysql database?

    Currently i use a pagination script that shows 10 rows per page but like the idea you have and was wondering if you could give us some clues, advice or example about implementing it with results coming from a db.

    Thanks

  37. 43

    In regards to the “Node cannot be inserted at the specified point in the hierarchy” code: “3″ error, which I was also getting with jQuery 1.5.2 – it seemed like the endless plugin was essentially trying to wrap the document itself in a which isn’t allowed (as of jquery 1.5.2 at least). I fixed the issue by putting everything inside the in a .

  38. 44

    @Mathew: Please use jQuery’s .get() method (or .ajax() method) for retrieving db results: http://api.jquery.com/jQuery.get/

    @Lee: Yes you are right, the latest version can be used on any DOM objects so please avoid using it on $(document) directly. :)

  39. 45

    Hi, it seems that only “document” works. Any idea?

  40. 46

    Hi, it seems that only “document” works. Any idea

  41. 47

    It seems that using it with combination with jmobile does not work. The 1.3 versions works just on the initial screen, if you follow a link and put it endlessScroll there it doesn’t. Any feedback? Thanks ;)

  42. 48

    Thanks for endlessscrolling.! Is there a way to remove the plugin after a partial postback? I am toggling between 2 usercontrols one have the endless scrolling the other a form. when i switch to the form the plugin is still attached to the document and give errors….

  43. 49

    @Manuel, you could try binding endless scroll to a DOM rather than to the document?

  44. 50

    Hi, I think that endlessscrolling needs an element with a fixed height to do the magic, is that right? If so my issue is that the DOM element I am adding my data expands vertically as data come in so i could not attach endlessscroll to it…. My issue now is that on the other usercontrol i need to attach endlessscroll to another callback function….. how could i switch the call to another callback function? thanks for the quick reply, it works its magic pretty good

  45. 51

    @Manuel, yes the DOM needs to have a fixed height. I don’t quite understand your other question though – do you mean you only want to ‘activate’ endless scroll via a callback from some other events? If it’s a DOM inserted by an AJAX call, you need to use either jQuery’s `live` or `delegate` to attach the endless scroll event to the DOM.

  46. 52

    Hi, I have 2 usercontrols, each one is pulling different data from 2 different tables. So I need to switch the callback function to a different one when i toggle between the 2 usercontrols. first user control does
    $(document).endlessScroll({
    fireOnce: true,
    callback: function (p) {
    GetPagedPost();
    }
    });
    then second user controls does
    $(document).endlessScroll({
    fireOnce: true,
    callback: function (p) {
    GetPagedData();
    }
    });
    each one calls a different callback function. how can i swap or recreate endless scroll with different parameters? thanks

  47. 53

    Hi, any ideas on how could i call endless with a given callback function and later when i switch to the other user control call endless with another callback function? My challenge is that it will be attached to $(document)..Any way to call remove() so that i could attach it again?

  48. 54

    how would you attach endless scroll with “live” from jquery?

  49. 55

    Or what event would you use to attach with bind()?? thanks

  50. 56

    Have a look at jQuery’s API. For example:

    $('#your_trigger_dom').live('click', function() {
    $('#your_endless_scroll_dom').endlessScroll();
    });

    You might want to create and bind a custom event if it’s not one of the common ones such as `click`.

  51. 57

    Thanks, no this bind() does not help me. Is there a way to programmaticaly swap the callback function? could this be implemented if it doesn’t?

  52. 58

    Hi Fred,
    could i modified your source code?

  53. 59

    Yes absolutely, feel free to take the source code and modify it. :)

  54. 61

    Hi Fred,
    The plugin is not workingon IE9..could you help find why? thanks

  55. 62

    Hi Fred,
    is there a way to get this working on the iPhone/iPad?

  56. 63

    Hi,

    could you PLEASE give an example of how to use ceaseFire?

    THANK YOU

  57. 64

    Hi, Any update for IE9 compatibility?

  58. 67

    it doesn’t work in IE8
    I get ‘console’ is undefined error

    Firefox OK but doesn’t start at 21 when scrolled, it resets to 1 (i.e. goes 19,20, 1,2,3,4 etc.)

    This happens with my local machine (using files from the zip download) and your online demo

    Have you not tested this?

  59. 68

    not sure if you got my post….

    IT DOESN’T WORK (online demo or downloaded file) in IE8 and sporadically in Firefox

  60. 69

    @BEN in IE8 you should use $(window).scroller(); as the scroll event is not supported on document object….

  61. 71

    Hi Fred,
    Thanks for the plugin. I am able to use the plugin and get most of my work done. I have a novice question. As of now I am able to get the content loaded using ajax when scrolled in either directions. But, i wish to have the endlessScroll working only when i scroll in the ‘down’ direction. How could i avoid endlessScroll behaviour when scrolled in the ‘up’ direction.

  62. 72

    @San, there might be a bug in the latest version, but if you don’t set `pagesToKeep` it should ignore upscrolling. If this doesn’t work, try version 1.6.0.

  63. 73

    @Fred, Thanks for the response. I am using v1.8.0 so i guess the bug you are referring to should be addressed in v1.8.0. I am not setting ‘pagesToKeep’. I could also notice the same behavior (loading content in ‘up’ direction) with the demo you provided. Anyhow I would try with v1.6.0 and let you know the results. Thanks

  64. 74

    @Fred, Can you please provide me the link to download v1.6.0. I could see the latest version and previous change logs but not a complete source file of v1.6.0

  65. 77

    How do you make it so that endless scroll continues calling? Mine only loads the callback once and then I can reach the end of the page :(

    This is what I have:
    jQuery(“#content”).endlessScroll({
    insertAfter: “.activityLogBox:last”,
    callback: function(p){
    console.log(p)
    jQuery.post(“temp/check_log?last=” + jQuery(“.activityLogBox:last”).attr(“id”),

    function(data){
    if (data != “”) {
    jQuery(“.activityLogBox:last”).after(data);
    }
    });
    }
    })

Trackbacks

  1. Endless Scroll jQuery plugin | webtoolkit4.me
  2. jQuery Endless Scroll Updated | Beyond Coding
  3. Web Design Trend Hunting -15 Amazing Infinite Scrolling Websites | Inspired Magazine
  4. Ich Twitter den Linktipp mal nicht
  5. jQuery Slideshow Lite Plugin Updated (Major Rewrite) « Best PHP Frameworks
  6. 18 Latest jQuery Plugins for Your Next Project | moreInet.com | Webdesign, Graphic Design Service in Pattaya
  7. 12 jQuery Infinite Scrolling(scroll & read) Plugins for Content Navigation | izabegraphicdesign.com
  8. Kỹ thuật lập trình .com » Jquery Effect » 10 jQuery Custom Scrollbar Plugins
  9. Delicious: 2009-07-01 | Blog Wolkanca
  10. Endless Scroll jQuery Plugin | Site
  11. 11 Great Infinite Scroll jQuery Plugin
  12. Grids, Paging and Infinite Scrolling « devioblog
  13. 11 Ajax Scroll with jQuery Plugins
  14. Endless Scroll using jQuery
  15. Barders' Portfolio » Blog Archive » 分页(pagination)是种过时的设计
  16. 16 jQuery Infinite Scroll Plugin
  17. 15 hiệu ứng jQuery "cuộn" tuyệt vời
  18. Awesome jQuery Infinite Scrolling Plugin Showcase | A1 Web Design Team :: Blog
  19. 15 Awesome jQuery Infinite Scrolling Plugin Showcase | Good Favorites
  20. Fresh and Useful Collection of JQuery Plugins | Web Design Burn
  21. Awesome jQuery Infinite Scrolling Plugin Showcase | Script Hunter
  22. 12 jQuery Infinite Scrolling(scroll & read) Plugins for Content .....::Boy9x::..Đẳng cấp BOY 9x Việt nam
  23. Aşağı Sonsuz Kaydırma jQuery | Infinite Scrolling | Yeni nesil İçerik Görüntüleme
  24. 14 Best Free Infinite Scrolling jQuery Plugins Examples and Tutorials

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>