JQuery Basic Rotator Plugin

by BinaryAcid

HTML

<div id="slider">
    <img src="http://lorempixel.com/300/200/food/1/" alt="1" />
    <img src="http://lorempixel.com/300/200/food/2/" alt="2" />
    <img src="http://lorempixel.com/300/200/food/3/" alt="3" />
    <img src="http://lorempixel.com/300/200/food/4/" alt="4" />
</div>

CSS

.rotator .item {
  display:block;
  position:absolute;
  top:0;
  right:0;
  bottom:0;
  left:0;
}

JavaScript

(function($) {
    $.fn.rotator = function(settings) {
        settings = jQuery.extend({
            interval: 3000,
            speed: 1000
        }, settings);

        return this.each(function() {
            var $t = $(this),
                $item = $t.children().addClass('item').hide();

            $t.addClass('rotator');

            if ($item.length > 1) {
                $item.first().addClass('current').fadeIn(settings.speed);
                setInterval(function() {
                    var c = $t.find('.current');
                    if (c.next().length === 0) {
                        c.removeClass('current').fadeOut(settings.speed);
                        $item.first().addClass('current').fadeIn(settings.speed);
                    } else {
                        c.removeClass('current').fadeOut(settings.speed).next().addClass('current').fadeIn(settings.speed);
                    }
                }, settings.interval);
            }
        });
    };
})(jQuery);

// Execute here!
$(function() {
    $('#slider').rotator();
});