HTML:
<div id="slider">
<div id="slider_cont">
<div id="slide">
<!-- wide content here -->
</div>
</div>
<div id="dots"><!--leave empty--></div>
</div>
CSS:
#slider_cont{
width:250px;
height:180px;
position:relative;
background:#eee;
overflow:hidden;
}
#slide{
position:absolute;
left:0;
width:2500px;
height:180px;
}
.dot{
cursor:pointer;
background:#444;
width:10px;
height:10px;
margin:4px 2px;
position:relative;
float:left;
border-radius:10px;
}
.dot.active{
background:#49a;
}
jQuery:
var steps = 10, // SET DESIRED n OF STEPS
delay = 3500,// SET DESIRED DELAY
galW = $('#slider_cont').width(),
c = 0, // counter
intv; // interval
// generate dots
for(i=0;i<steps;i++){
$('#dots').append('<div class="dot" />');
}
$('#dots .dot').eq(c).addClass('active');
// ANIMATIONS function
function anim(){
c=c%steps; // reset counter to '0' if greater than steps
$('#slide').stop().animate({left: '-'+galW*c},800);
$('#dots .dot').removeClass('active').eq(c).addClass('active');
}
// AUTO SLIDE function
function auto(){
intv = setInterval(function(){
c++; anim();
},delay);
}
auto(); // kick auto slide
// PAUSE slider on mouseenter
$('#slider').on('mouseenter mouseleave',function(e){
var evt = e.type=='mouseenter'? clearInterval(intv) : auto();
});
// CLICK dots to animate
$('#dots').on('click','.dot',function(){
c=$(this).index(); anim();
});
希望我对代码进行了很好的评论,请问您是否有问题