JQuery Basic Rotator Plugin
by Taufik Nurrohman
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
body {
padding:50px;
background-color:#D8D3AF;
}
.rotator {
background-color:whitesmoke;
position:relative;
width:300px;
height:200px;
margin:0px auto;
border:5px solid white;
-webkit-box-shadow:0px 1px 3px rgba(0,0,0,0.4);
-moz-box-shadow:0px 1px 3px rgba(0,0,0,0.4);
box-shadow:0px 1px 3px rgba(0,0,0,0.4);
}
.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();
});