$(function() { var sWidth = $("#focus").width(); //获取焦点图的宽度(显示面积) var len = $("#focus ul li").length; //获取焦点图个数 var index = 0; var picTimer; var fSpeed=4000;//毫秒 //按钮 var btn = "
"; for(var i=0; i < len; i++) { btn += ""; } btn += "
"; $("#focus").append(btn); //为按钮添加鼠标滑入事件 $("#focus .btn span").mouseenter(function() { index = $("#focus .btn span").index(this); showPics(index); }).eq(0).trigger("mouseenter"); //计算出外围ul元素的宽度 $("#focus ul").css("width",sWidth * (len)); //焦点图鼠标事件 $("#focus").hover(function() { clearInterval(picTimer); },function() { picTimer = setInterval(function() { showPics(index); index++; if(index == len) {index = 0;} },fSpeed); }).trigger("mouseleave"); //显示图片函数,根据接收的index值显示相应的内容 function showPics(index) { //普通切换 var nowLeft = -index*sWidth; //根据index值计算ul元素的left值 $("#focus ul").stop(true,false).animate({"left":nowLeft},200); //通过animate()调整ul元素滚动到计算出的position $("#focus .btn span").removeClass("on").eq(index).addClass("on"); //为当前的按钮切换到选中的效果 } });