Infinite Scroll jQuery Plugin

Note: This documentation is currently out-of-date and will be updated soon. In the mean time, please refer to the GitHub README and Wiki.

This plugin aims to progressively enhance your page. Your navigation/pagination elements should be present in the HTML for non-js users, but the plugin will utilize those links to build out a more rich browsing experience.

Download the jQuery plugin

Version 1.5.100504
jquery.infinitescroll.js and minified are now on GitHub

Usage

Minimum required configuration

This is the minimum amount of configuration you can do, if you want things to work:

// infinitescroll() is called on the element that surrounds 
// the items you will be loading more of
  $('#content').infinitescroll({
 
    navSelector  : "div.navigation",            
                   // selector for the paged navigation (it will be hidden)
    nextSelector : "div.navigation a:first",    
                   // selector for the NEXT link (to page 2)
    itemSelector : "#content div.post"          
                   // selector for all items you'll retrieve
  });

All options

// usage:
// $(elem).infinitescroll(options,[callback]);
 
// infinitescroll() is called on the element that surrounds 
// the items you will be loading more of
$('#content').infinitescroll({
 
  navSelector  : "div.navigation",            
                 // selector for the paged navigation (it will be hidden)
 
  nextSelector : "div.navigation a:first",    
                 // selector for the NEXT link (to page 2)
 
  itemSelector : "#content div.post",          
                 // selector for all items you'll retrieve
 
  debug        : true,                        
                 // enable debug messaging ( to console.log )
 
  loadingImg   : "/img/loading.gif",          
                 // loading image.
                 // default: "http://www.infinite-scroll.com/loading.gif"
 
  loadingText  : "Loading new posts...",      
                 // text accompanying loading image
                 // default: "<em>Loading the next set of posts...</em>"
 
  animate      : true,      
                 // boolean, if the page will do an animated scroll when new content loads
                 // default: false
 
  extraScrollPx: 50,      
                 // number of additonal pixels that the page will scroll 
                 // (in addition to the height of the loading div)
                 // animate must be true for this to matter
                 // default: 150
 
  donetext     : "I think we've hit the end, Jim" ,
                 // text displayed when all items have been retrieved
                 // default: "<em>Congratulations, you've reached the end of the internet.</em>"
 
  bufferPx     : 40,
                 // increase this number if you want infscroll to fire quicker
                 // (a high number means a user will not see the loading message)
                 // new in 1.2
                 // default: 40
 
  errorCallback: function(){},
                 // called when a requested page 404's or when there is no more content
                 // new in 1.2                   
 
  localMode    : true
                 // enable an overflow:auto box to have the same functionality
                 // demo: http://paulirish.com/demo/infscr
                 // instead of watching the entire window scrolling the element this plugin
                 //   was called on will be watched
                 // new in 1.2
                 // default: false
 
    },function(arrayOfNewElems){
 
     // optional callback when new content is successfully loaded in.
 
     // keyword `this` will refer to the new DOM content that was just added.
     // as of 1.5, `this` matches the element you called the plugin on (e.g. #content)
     //                   all the new elements that were found are passed in as an array
 
});

Custom trigger, non-automatic. Twitter-style

In 1.4 you can trigger the loading of the next page of content at will. You’ll first unbind the default behavior. And then trigger the next pull whenever you like..

// unbind normal behavior. needs to occur after normal infinite scroll setup.
$(window).unbind('.infscr');
// call this whenever you want to retrieve the next page of content
// likely this would go in a click handler of some sort
$(document).trigger('retrieve.infscr');

Demo of the triggered infinite scroll functionality.

How does it work?

There is a little known feature in the .load() method that lets you specify the CSS selector of the html you want to include. jQuery will load in any local URL, then parse the html and grab only the elements you’ve defined with your selector. This allows for some pretty fun shit: client-side transclusions (a la purple include) ; and some really kickass shit when you combo it with a local php proxy.

This is really the meat of the code:

// load all post divs from page 2 into an off-DOM div
$('
').load('/page/2/ #content div.post',function(){ 
    $(this).appendTo('#content');    // once they're loaded, append them to our content area
});

So it basically leverages that load() method at its core. It’s basically scraping your existing page structure, which means you don’t need to code any custom backend stuff to enable this functionality! Booyah, right?

176 Responses to “Infinite Scroll jQuery Plugin”

  1. Michael C. says:

    It seems as though this plugin may have fallen into the abandonware category.

    Right out of the gate the instructions were wrong, especially regarding the options for the “Loading…” and “No more…” message strings. I had to dig into the code to find out that the ‘options’ structure and option names have been reworked and are no longer aligned with the docs.

    The big problem I’m having right now is that I’m able to keep on scrolling, WAAAAY WAAY past the pagination limit. On my demo page, I have 4 total pages. When I scroll down, new data does indeed load; however, it loads infinitely the more I scroll (i.e.: 4 + INF). How is this plugin supposed to know when to stop loading more pages? Guess I’ll have to dig into the code again. Ugh. Someone experienced, please fork this and pick the project back up off the floor, please.

  2. Its funny you complain about it being abandonware and imply that someone in-experienced is working on it since it’s in Paul Irish’s repository on github. Have you thought of opening a github issue?

  3. Paul Irish says:

    Hey Michael,

    The code is looking good right now https://github.com/paulirish/infinite-scroll

    Although the docs are lackign. also this site hasnt seen an update in a while. Sorry about that. The team is working on it.

  4. Amy says:

    I can’t get this to work, I think it’s something to do with my A class, but I’m not sure what to do. help?

    content css selector: #blog
    post css selector: #blog div.post

    navigation links selector: div#nextprev

    previouspostsselector: div#nexprev .nextpostslink
    (yes next is right using the function class provider by yoast)

    What am I doing wrong? I get to the end of my page and it just does nothing. Pretty sure it’s something to do with the A selector, but I can’t work out what?

    Cheers for any help!

  5. marsd says:

    loadingText : “Loading new posts…”,

    This does not seem to work at all.

  6. jebediah says:

    re: loadingText

    As noted above the syntax changed for the 2.x versions, it’s best to look at everything in the source at the github repository, but assuming the site dosn’t eat the code, for the loading stuff right now it’s:

    $.infinitescroll.defaults = {
    loading: {
    finished: undefined,
    finishedMsg: “Congratulations, you’ve reached the end of the internet.“,
    img: “http://www.infinite-scroll.com/loading.gif”,
    msg: null,
    msgText: “Loading the next set of posts…“,
    selector: null,
    speed: ‘fast’,
    start: undefined
    },

  7. Bostjan says:

    Hi,

    I’m wondering if there is a way of having multiple scrolls on one page. My situation is like this:
    I have a tab control on one page that switches between different divs. I want those divs to scroll seperately using the infinite scroll plugin. The thing is, that they’re all in the same position of course (because of the tab control). If there is only one scroll it works, but as soon as I put in a new one they conflict and both start loading when I reach the end of the page.

    Is there a way to disable scrollers on the hidden divs? Sample of what I’m talking about is here:
    http://vspomin.kreatorij.com/sl/podrobnosti-o-spominu/3/janez/ruzej

    Check the “Post mortum” and “Posvetila” tabs.

    Thank you for an answer.

  8. luke says:

    Hi all,

    I’m working on the documentation today. It’s definitely overdue, and I apologize for the delay.

    I wouldn’t classify this as “abandonware”, but please keep in mind that our team is extremely small and we all have a lot going on. Remember, this is an open source project. I can’t speak for Paul or the others, but if there are other people who want to help out with the plugin, documentation, new site, etc., having your help would be very appreciated.

  9. sussegaduss says:

    Maybe its just me, but i dont think the documentation is clear enough to show how to infinite scrolling when i make different ajax requests each time i reach the end of the page…
    all i see is these selectors…

    nextSelector : “div.navigation a:first”,

    // selector for the NEXT link (to page 2)

    but what if i need to make ajax requests??? is there a parameter like this: ???
    urlToNextPage: http://www.vvv.com/?page=1&anotherparameter=3

    it would be more straitfoward…

    how can i make that works?

    someone???

  10. Bostjan says:

    Hi,

    is there any chance that someone can take a look at my post from October 28th. I could really use some help on this.

    Thank you in advance and I appreciate all the work you put into this plugin.

  11. Bostjan says:

    After looking around the .js file I noticed a pause method, which seems to be exactly what I need but I can’t figure out what would be the correct way of calling it? Can you help me with that?

    Thank you.

  12. Deska Podłogowa…

    [...]Infinite Scroll jQuery Plugin « Infinite Scroll | jQuery plugin, WordPress plugin, interaction design pattern[...]…

  13. Quang Lee says:

    can anyone help me, please? i can’t make it work. i believe i have problems with the configuration in the wordpress plugin window
    this is what i have in my index.php
    so i need to know what do i have to write in this fields:

    Content CSS Selector:
    Post CSS Selector:
    Navigation Links CSS Selector:
    Previous posts CSS Selector:
    THANKS A LOT.

    <a href=”” rel=”bookmark”>
    <!– by –>

    comment_status) : ?>

    Not Found
    Sorry, but you are looking for something that isn’t here.

  14. Quang Lee says:

    <a href=”” rel=”bookmark”>
    <!– by –>

    comment_status) : ?>

    Not Found
    Sorry, but you are looking for something that isn’t here.

  15. Quang Lee says:

    can anyone help me, please? i can’t make it work. i believe i have problems with the configuration in the wordpress plugin window
    this is what i have in my index.php
    so i need to know what do i have to write in this fields:

    Content CSS Selector:
    Post CSS Selector:
    Navigation Links CSS Selector:
    Previous posts CSS Selector:
    THANKS A LOT.

    <a href=”” rel=”bookmark”>
    <!– by –>

    comment_status) : ?>

    Not Found
    Sorry, but you are looking for something that isn’t here.

  16. Adam says:

    Is there a way to get this to work with a Custom Post Type, not just the “posts” page?

  17. Eugene S. says:

    I’m experiencing very strange situation: I have 3 pages: /page/1, /page/2, /page/3. They have identical structure, only content is different. Each page has Next link.
    If I’m starting loading from /page/1 everything works just fine, and I can scroll down to /page/3 with no problem.
    But, if I open /page/2 then it just doesn’t work, as if nothing is happening.
    Another wired thing: I have different page but with similar structure, which also has Next link. If the Next link is /page/2 it works, and I’m able to scroll. But if the Next link if /page/1 or /page/3 it is not loading.
    Any idea how it could be even possible? Any info is appreciated.
    Thank you.

  18. max engel says:

    is there a way to control how close to the bottom the refresh occurs? i thought this was bufferPx, but that seems to control how much of the page is loaded, by not how close to the bottom the person is when additional content is loaded. i am implementing it on http://www.brickd.com.

  19. jaakul says:

    Tried to make it work in plain jQuery with Twitter style custom trigger. Couldn’t figure it out until tried direct url to plugin (http://www.infinite-scroll.com/wp-content/plugins/infinite-scroll/jquery.infinitescroll.js) instead of downloaded local file and it worked. So apparently there’s something broken in downloadable version?

  20. Choco says:

    Hello,
    Thanks for this plugin…

    I’d like to see it in action on my blog but every time I try to save in the admin panel I get a blank page with this message: ‘Are you sure you want to do this?’ with any buttons or options :( So I can’t change settings…

    I’m using the Thesis theme on wordpress 3.2.1

    Please help!

  21. sathia says:

    for some reason the plugin tries to guess the next URL instead of using the one i feed from the HREF attribute.
    is there any way to disable this bug? imo it should be parametrized, with the default set to false.

    thanks

  22. sathia says:

    re: re: loadingText

    doesn’t work for me as well, tried both constants

  23. wordpress says:

    wordpress…

    [...]Infinite Scroll jQuery Plugin « Infinite Scroll | jQuery plugin, WordPress plugin, interaction design pattern[...]…

  24. terry says:

    finishedMsg : “End of the Page”

    this option doesn’t work as well

  25. OnlyC says:

    I’ve tried to install it to my asp.net project for 2 days but I can’t :(
    My page:
    Default.aspx: http://pastebin.com/ibayfKzK
    Default.aspx.cs: http://pastebin.com/bt1DLuua

    Pls help me asap.