就性能而言,移动滚动条不会为您提供最佳结果。
但是,要回答您的问题:
var Slider = {
incr: 3, //everytime go to 3rd image from current
imageNumber: 0, //this keeps track of index of image to go to
intervalDuration: 3000, //interval between each scroll
imageMargin: 20, //margin that you set between images
nextImage: null, //next image object, keeps updating,
updateNextImage: function(){
$(Slider.nextImage).removeClass('next');
Slider.imageNumber += Slider.incr;
$('.row:first img:nth-child(' + Slider.imageNumber + ')').addClass('next');
Slider.nextImage = $('img.next');
}
}
$(document).ready(function(){
//set next item first
Slider.updateNextImage();
setInterval(function(){
try{
$('.gallery').animate(
{ scrollLeft: $(Slider.nextImage).position().left + $('.gallery').scrollLeft() + $(Slider.nextImage).outerWidth() + Slider.imageMargin }, //Scrolls to the element
300, function(){
//update next item
Slider.updateNextImage();
});
} catch(e){
//code to restart slider
Slider.imageNumber = 0;
$('.gallery').animate({scrollLeft: 0});
Slider.updateNextImage();
}
}, Slider.intervalDuration);
});
为了获得更多控制、更简洁的编码和许多其他形式的滑块,我建议使用现有的 jQuery 插件来实现这一点:http ://www.woothemes.com/flexslider/
一切顺利。