/*! * jquery scrollbox * (c) 2009-2013 hunter wu * mit licensed. * * http://github.com/wmh/jquery-scrollbox */ (function($) { $.fn.scrollbox = function(config) { //default config var defconfig = { linear: false, // scroll method startdelay: 2, // start delay (in seconds) delay: 3, // delay after each scroll event (in seconds) step: 5, // distance of each single step (in pixels) speed: 32, // delay after each single step (in milliseconds) switchitems: 1, // items to switch after each scroll event direction: 'vertical', distance: 'auto', autoplay: true, onmouseoverpause: true, paused: false, queue: null }; config = $.extend(defconfig, config); config.scrolloffset = config.direction === 'vertical' ? 'scrolltop' : 'scrollleft'; if (config.queue) { config.queue = $('#' + config.queue); } return this.each(function() { var container = $(this), containerul, scrollingid = null, nextscrollid = null, paused = false, backward, forward, resetclock, scrollforward, scrollbackward; if (config.onmouseoverpause) { container.bind('mouseover', function() { paused = true; }); container.bind('mouseout', function() { paused = false; }); } containerul = container.children('ul:first-child'); scrollforward = function() { if (paused) { return; } var curli, i, newscrolloffset, scrolldistance, thestep; curli = containerul.children('li:first-child'); scrolldistance = config.distance !== 'auto' ? config.distance : config.direction === 'vertical' ? curli.height() : curli.width(); // offset if (!config.linear) { thestep = math.max(3, parseint((scrolldistance - container[0][config.scrolloffset]) * 0.3, 10)); newscrolloffset = math.min(container[0][config.scrolloffset] + thestep, scrolldistance); } else { newscrolloffset = math.min(container[0][config.scrolloffset] + config.step, scrolldistance); } container[0][config.scrolloffset] = newscrolloffset; if (newscrolloffset >= scrolldistance) { for (i = 0; i < config.switchitems; i++) { if (config.queue && config.queue.haschildnodes() && config.queue.getelementsbytagname('li').length > 0) { containerul.append(config.queue.getelementsbytagname('li')[0]); containerul.remove(containerul.children('li:first-child')); } else { containerul.append(containerul.children('li:first-child')); } } container[0][config.scrolloffset] = 0; clearinterval(scrollingid); if (config.autoplay) { nextscrollid = settimeout(forward, config.delay * 1000); } } }; // backward // 1. if forwarding, then reverse // 2. if stoping, then backward once scrollbackward = function() { if (paused) { return; } var curli, i, lilen, newscrolloffset, scrolldistance, thestep; // init if (container[0][config.scrolloffset] === 0) { lilen = containerul.children('li').length; for (i = 0; i < config.switchitems; i++) { containerul.children('li:last-child').insertbefore(containerul.children('li:first-child')); } curli = container.children('li:first-child'); scrolldistance = config.distance !== 'auto' ? config.distance : config.direction === 'vertical' ? curli.height() : curli.width(); container[0][config.scrolloffset] = scrolldistance; } // new offset if (!config.linear) { thestep = math.max(3, parseint(container[0][config.scrolloffset] * 0.3, 10)); newscrolloffset = math.max(container[0][config.scrolloffset] - thestep, 0); } else { newscrolloffset = math.max(container[0][config.scrolloffset] - config.step, 0); } container[0][config.scrolloffset] = newscrolloffset; if (newscrolloffset === 0) { clearinterval(scrollingid); if (config.autoplay) { nextscrollid = settimeout(forward, config.delay * 1000); } } }; forward = function() { clearinterval(scrollingid); scrollingid = setinterval(scrollforward, config.speed); }; backward = function() { clearinterval(scrollingid); scrollingid = setinterval(scrollbackward, config.speed); }; resetclock = function(delay) { config.delay = delay || config.delay; cleartimeout(nextscrollid); if (config.autoplay) { nextscrollid = settimeout(forward, config.delay * 1000); } }; if (config.autoplay) { nextscrollid = settimeout(forward, config.startdelay * 1000); } // bind events for container container.bind('resetclock', function(delay) { resetclock(delay); }); container.bind('forward', function() { cleartimeout(nextscrollid); forward(); }); container.bind('backward', function() { cleartimeout(nextscrollid); backward(); }); container.bind('speedup', function(speed) { if (typeof speed === 'undefined') { speed = math.max(1, parseint(config.speed / 2, 10)); } config.speed = speed; }); container.bind('speeddown', function(speed) { if (typeof speed === 'undefined') { speed = config.speed * 2; } config.speed = speed; }); }); }; }(jquery));