Content Scroller 1.0

Created Sunday 25th of April 2010 by Andreas Lagerkvist
Copyright © 2010 Andreas Lagerkvist (andreaslagerkvist.com)

Please have a look at the "Other Resources" for bug reports or further help on jQuery. Do not post bug reports or feature requests as comments to unrelated articles. If bug reporting on bugtracker.a-framework.org fails, e-mail me.

What it Does

Use this plug-in to make a list of elements only show one at a time and every now and then scroll to the next one. The plug-in scrolls in infinty, starting with the first after the last.

How to Use

jQuery('#recent-news ul').contentScroller(); would make the > li:s in #recent-news ul scroll.

Example

  • Content 1
  • Content 2
    On two lines
  • Content 3

Example Code

HTML

<div id="jquery-content-scroller-example">
<
ul>
    <
li>Content 1</li>
    <
li>Content 2<br/>On two lines</li>
    <
li>Content 3</li>
</
ul>
</
div>

JS

jQuery('#jquery-content-scroller-example ul').contentScroller();

Source Code

(function ($) {
    $.
fn.contentScroller = function (conf) {
        var 
config = $.extend({
            
itemSelector:    '> li'
            
pause:            7500
            
duration:        1000
            
easing:            'easeOutQuad'
            
className:        'jquery-content-scroller'
        
}, conf);

        return 
this.each(function () {
            var 
pos                0;
            var 
itemsList        = $(this);
            var 
items            itemsList.find(config.itemSelector);
            var 
scrollContainer    itemsList
                                    
.wrap('<div class="' config.className '"/>')
                                    .
parent()
                                    .
scrollTo('0', {axis'y'})
                                    .
css({heightitems.eq(0).outerHeight() + 'px'overflow'hidden'});

            
// Add first item to end of list as well so that we can scroll in a loop
            
$('<li>' items.eq(0).html() + '</li>').appendTo(itemsList);

                
items            itemsList.find('> li');
            var 
numItems        items.length;

            
setInterval(function () {
                
pos++;
                var 
afterScroll = function () {};

                
// We're scrolling to the last item
                
if (pos == numItems 1) {
                    
afterScroll = function () {
                        
scrollContainer.scrollTo('0', {axis'y'});
                        
pos 0;
                    };
                }

                
scrollContainer.scrollTo(items.eq(pos), {
                    
axis:        'y'
                    
duration:    config.duration
                    
easing:        config.easing
                    
onAfter:    afterScroll
                
}).css('height'items.eq(pos).outerHeight() + 'px');
            }, 
config.pause);
        });
    };
})(
jQuery);

Download

Plug-in

Requires

Random jQuery Plug-ins

  • Text Shadow

    Frankly a rather crappy text-shadow plug-in (can one be done well? :) but nevertheless it does the job in some cases.

    Details

  • Content Scroller

    Use this plug-in to make a list of elements only show one at a time and every now and then scroll to the next one. The plug-in scrolls in infinty, sta...

    Details

  • View More

    This plug-in allows an element's contents to be hidden untill the user clicks a certain element. It works exactly like the HTML5 details and summary e...

    Details

More Plug-ins

Recent Comments

  1. Andreas on "SleekPHP User Handling"

    Thank you for showing interest :) Were the docs en...

  2. ciptard on "SleekPHP User Handling"

    Ok, thank you

  3. Andreas on "SleekPHP User Handling"

    I might write a "getting started" article for Slee...