http://jsfiddle.net/EgkFq/5/
(function slider(){
var slides = $("#slider > img");
var currentIndex = 0;
var slideCount = slides.length;
var timePerSlide = 5000;
var fadeDuration = 1000;
var nextSlide = function(){
var nextIndex = currentIndex + 1;
if (nextIndex == slideCount)
nextIndex = 0;
$(slides[currentIndex ]).fadeOut(fadeDuration);
$(slides[nextIndex ]).fadeIn(fadeDuration);
currentIndex = nextIndex;
setTimeout(nextSlide, timePerSlide);
};
setTimeout(nextSlide, timePerSlide);
})();
插件 shmugin。
这是一个如何在“滑块”中居中图像的示例http://jsfiddle.net/EgkFq/9/
(function slider(){
var slides = $("#slider > img");
var currentIndex = -1;
var slideCount = slides.length;
var timePerSlide = 5000;
var fadeDuration = 1000;
var nextSlide = function(){
var nextIndex = currentIndex + 1;
if (nextIndex == slideCount)
nextIndex = 0;
var me = $(slides[currentIndex]);
var nxt = $(slides[nextIndex]);
var w = nxt.width();
var h = nxt.height();
me.fadeOut(fadeDuration);
nxt.fadeIn(fadeDuration);
nxt.css({
"left":"50%",
"margin-left":w/2 * -1,
"top":"50%",
"margin-top": h/2 * -1
});
currentIndex = nextIndex;
setTimeout(nextSlide, timePerSlide);
};
setTimeout(nextSlide, 0);
})();