Tip: Click lines to highlight, hold ctrl/cmd to multi-select

Block rotator (10-Jul @ 16:28)

Usage: $('blockquote').rotate(2000); // in milliseconds

remy

Syntax Highlighted Code

  1. $.fn.rotate = function (delay) {
  2.     var rotators = this,
  3.         n = this.length,
  4.         current = 0,
  5.         timer = null;
  6.  
  7.     rotators.hide().eq(current).show();
  8.     timer = setInterval(function () {
  9.         current++;
  10.         if (current == n) current = 0;
  11.         rotators.hide().eq(current).show();
  12.     }, delay);
  13.    
  14.     return rotators;
  15. };

Plain Code

$.fn.rotate = function (delay) {
    var rotators = this, 
        n = this.length, 
        current = 0,
        timer = null;

    rotators.hide().eq(current).show();
    timer = setInterval(function () {
        current++;
        if (current == n) current = 0;
        rotators.hide().eq(current).show();
    }, delay);
    
    return rotators;
};

Permalink: http://codedumper.com/block-rotator