Infinite Scroll jQuery Plugin

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.1.2008.09.25
jquery.infinitescroll.js (5.5k)
jquery.infinitescroll.min.js (2.4k)

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>"
 
                  },function(){
 
                   // optional callback when new content is successfully loaded in.
                   // keyword 'this' will refer to the new DOM content that was just added.
  });

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
$('<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?

no comments yet.

0 Comments »

No comments yet.

Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use basic HTML. Wrap code in <pre lang="javascript"></pre> .