$(function(){ var $show_img = $("#show-img>.vmc-centered").width();//总宽度 var $img = $("#show-img>ul>li img"); var $ul = $("#show-img>ul"); var img_width = ($show_img/5)-10;//算出设置图片的宽度 $img.width(img_width);//设置图片宽度 //设置定时器 var time = setInterval(rightMove,2500); //防止多次点击 var btn = true; //点击向左按钮 $("#show-img .left_btn").click(function(){ clearInterval(time);//清除定时器 leftMove(); if(btn==true){ setTimeout(function(){ time=setInterval(leftMove,2500); btn=true },1500);//重启定时器 } btn=false; }); //点击向右按钮 $("#show-img .right_btn").click(function(){ clearInterval(time);//清除定时器 rightMove() if(btn==true){//防止多次点击 setTimeout(function(){ time=setInterval(rightMove,2500); btn=true },1500);//重启定时器 } btn=false; }); //向左移动 function leftMove(){ $ul.find("li").first().animate({ 'margin-left':-img_width-10+'px' },1000,function(){ var childLi = $ul.find("li").first().clone(false).css({"margin-left":'0px'}); $ul.append(childLi); $(this).remove() }); } //向右移动 function rightMove(){ var childLi = $ul.find("li").last().clone(false).css({"margin-left":-img_width-10+'px'}); $ul.prepend(childLi); $ul.find("li").last().remove(); $ul.find("li").first().animate({ 'margin-left':'0px' },1000,function(){ $(this).css({ "margin-left":'0px' }); }); } })