Here's a simple plugin function that will scroll endlessly through list items:
Update Now two at a time:
$.fn.scrollList = function (delay) { delay = delay || 2000; var animateList = function ($list) { //get first child var $first = $list.children('li:first'); var width = $first.outerWidth(true); //animate first two off the screen $first.animate({ 'margin-left': $list.width() * -1 }, // on animation complete function () { //reset and move to the end of the list $(this).css('margin-left', 0).add($(this).next()).appendTo($list); //start again after delay setTimeout(function () { animateList($list) }, delay); }); }; return this.each(function () { var $that = $(this) setTimeout(function () { animateList($that); }, delay); }); };
You can call it like this:
$('.container ul').scrollList();
Here is a demo script
Please note that it requires certain styles to work properly, especially for elements requiring margin-left:0
, as this property is animated
Also you need to remove the spaces between the <li>
tags to avoid extra space between them, check this answer for different ways to do this
source share