Skip to content
gyulalaszlo edited this page Sep 14, 2010 · 21 revisions

Due To GITHUB’s freaky Textile-based wiki, and my habit of doing documentation in Markdown, the example code in this wikipage is broken: the angle brackets ( < ) and ( > ) are converted into their HTML symbol equivalent. Every source code part has these funky &lt; and &gt; symbols, which in reality should be angle brackets. You have been warned :)…

CScroller

An JavaScript Continous/Endless/Infinite scroller

CScroller enables you to add endless (infinite) scrolling to almost
any webpage, without the need to generate special url-s or rewriting
the existing code. All this niceness with only a few lines of Javascript.

Requirements:

  • Prototype: http://prototypejs.org
    Some recent version of prototype.js (probably any prototype.js you can find
    lying around).

Targeted platforms

CScroller runs on any browser, where Prototype works:

  • Microsoft Internet Explorer for Windows, version 6.0 and higher
    (probably 5.5)
  • Mozilla Firefox 1.5 and higher
  • Apple Safari 2.0 and higher
  • Opera 9.25 and higher
    (also confirmed working with Opera 9.20 bundled with Adobe CS3)

Licence

The MIT License

© Copyright 2008 Gyula László

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Using CScroller

To use CScroller in your application, download the latest release from the
CScroller source site and copy
cscroller.js to a suitable location. Then include it in your HTML
like so:

    &lt;script type="text/javascript" src="/path/to/prototype.js"&gt;&lt;/script&gt;
    &lt;script type="text/javascript" src="/path/to/cscroller.js"&gt;&lt;/script&gt;

Usage by example:

    new thepaw.ContinousScroller("recommended_programs",{ url:"/top10/$page_num$" }, {
        loading_placeholder:'&lt;div class="white_box"&gt;Blah-blah&lt;/div&gt;',
        check_interval:0.5,
        failiure_message:'&lt;b&gt;Something terrible happened.&lt;/b&gt;&lt;br/&gt;'+
            'We cannot help you get any more content. Check your internet connection.'
    })

    new thepaw.ContinousScroller("recommended_programs", { url:"?page=$page_num$"}, {
        onSegmentLoadComplete:function(transport) {
            alert("downloaded stuff :" + transport)
        },
        loading_placeholder: "Loading more content, please wait..."
    })

For a live example, go to “sziget.FM”.

Constructor

    new thepaw.ContinousScroller(content_div_id, url_generator, options)

Create a continous scroller. Only one instance per page…

Parameters:

  • contentdivid:
    The div to serve the content into.
  • urlgenerator:
    The url generator to use. See “The default url generator”
    bit bellow.

Options:

  • check_interval:
    The interval between checks (seconds). Default is 0.2

  • trigger_height:
    The maximum distance from the bottom where the content
    update gets triggered (pixels). Default is 1000

  • failiure_message:
    The message to show on the bottom when the scroller
    can’t load the next page.

  • loading_placeholder:
    The placeholder text to show when the loading is still
    in progress.

Callbacks:

  • onSegmentLoadStart:
    function() { ... }
    Callback when starting to load a new segment.

  • onSegmentLoadComplete:
    function(transport) { ... }
    Callback when the AJAX request completed. The transport is the
    raw Protype transport. (use transport.responseText
    to get the raw response text)

  • onSegmentInsertComplete:
    function() { }
    Callback when the new contents are inserted to the bottom.

  • onSegmentLoadFaliure:
    function(transport) { ... }
    Callback when the AJAX request fails. The transport is the
    raw Protype transport. (use transport.responseText
    to get the raw response text)

The default url generator

A generator is a hash with a next() function which returns
the url of the next segment to load. The second parameter of
the ContinousScroller accepts hash or a custom url generator:

Examples:

// Using the default url generator
// we don't define next()

{  url:"/pages/$page_num$.html"  } 
    // =&gt; '/pages/1.html', '/pages/2.html', ....

{  start_index:10, url:"/pages/$page_num$"  } 
    // =&gt; '/pages/10', '/pages/11', '/pages/12', ...

{  start_index:10, default_spacing:20, url:"/pages/$page_num$"  } 
    // =&gt; '/pages/10', '/pages/20', '/pages/30', ...

// Using a custom url generator
// we simply define next()

{  next:function() { return "?page_idx=" + Math.round(Math.random() * 100) } } 
    // =&gt; '?page_idx=57', '?page_idx=19', '/pages/81', ...

{  
    idx = 0,
    next:function() {
        return "items?from=" + this.idx + "&amp;until=" + (this.idx += 10) 
    }
} 
    // =&gt; 'items?from=0&amp;until=10', 'items?from=0&amp;until=10', '/pages/30'

Credits