$(function(){ /*未元素的首尾添加补白*/ var $panels = $('#slider .scrollcontainer > li'); var $parent=$panels.parent();//或许当前li的最近一级的父元素 var $length=$panels.length;//获取指定length值 var $first=$panels.eq(0).clone().addclass("panel cloned").attr("id",'panel_'+($length+1));//获取第一个元素并复制 var $last=$panels.eq($length-1).clone().addclass("cloned").attr("id",'panel_0');;//获取最后一个元素并复制 $parent.append($first);//将li序列中的第一个添加到ul元素中的最后一个 $("#slide02").scrollleft(0); $parent.prepend($last);//将li序列中的最后一个添加到ul元素中的第一个 var totalpanels = $(".scrollcontainer").children().size();//所有子元素的数字,滚动元素的个数 7 var regwidth = $(".panel").css("width");//获取li元素的宽度 var regimgwidth = $(".panel img").css("width");//获取其中图片的宽度 var movingdistance = 310;//每次移动的距离 var curwidth = 380;//当前中间li的宽度为350px var curheight =260;//当前中间li的高度未312 var curimgwidth =380; var curimgheight =260; var othersw =290;//其他li的宽度 var othersh =198;//高度 var othersimgw =290;//其他li的宽度 var othersimgh =198;//高度 var $panels = $('#slider .scrollcontainer > li');//此处作用是将复制进来补白的元素重新赋值 var $container = $('#slider .scrollcontainer'); $panels.css({'float' : 'left','position' : 'relative'}); $("#slider").data("currentlymoving", false);//是否正在运动中 $container.css('width', (($panels[0].offsetwidth+30) * $panels.length) + 60 ).css('left','0');//计算容器的总的宽度 ps:25为margin值 offsetwidth var scroll = $('#slider .scroll').css('overflow', 'hidden'); function returntonormal(element) {//li元素返回到正常状态 $(element).animate({ width: othersw,height: othersh}).find("img").animate({width:othersimgw,height:othersimgh}); }; function growbigger(element) {//当前元素之间变大 $(element).addclass("current").animate({ width: curwidth,height:curheight}).siblings().removeclass("current") .end().find("img").animate({width:curimgwidth,height:curimgheight}) } //direction true = right, false = left function change(direction) { //if not at the first or last panel if((direction && !(curpanel < totalpanels-2)) || (!direction && (curpanel <= 1))) { return false; } //if not currently moving if (($("#slider").data("currentlymoving") == false)) { $("#slider").data("currentlymoving", true); var next = direction ? curpanel + 1 : curpanel - 1; var leftvalue = $(".scrollcontainer").css("left"); var movement = direction ? parsefloat(leftvalue, 10) - movingdistance : parsefloat(leftvalue, 10) + movingdistance; $(".scrollcontainer").stop().animate({"left": movement}, function() { $("#slider").data("currentlymoving", false); }); returntonormal("#panel_"+curpanel); growbigger("#panel_"+next); curpanel = next; //remove all previous bound functions $("#panel_"+(curpanel+1)).unbind(); //go forward $("#panel_"+(curpanel+1)).click(function(){ change(true); }); //remove all previous bound functions $("#panel_"+(curpanel-1)).unbind(); //go back $("#panel_"+(curpanel-1)).click(function(){ change(false); }); //remove all previous bound functions $("#panel_"+curpanel).unbind(); } } // set up "current" panel and next and prev 设置当前元素和上下 growbigger("#panel_1"); var curpanel = 1; $("#panel_"+(curpanel+1)).click(function(){ change(true);return false;}); $("#panel_"+(curpanel-1)).click(function(){ change(false);return false;}); //when the prev/next arrows are clicked $("#slider .next").click(function(){ change(true);}); $("#slider .prev").click(function(){ change(false);}); $(window).keydown(function(event){//键盘方向键控制 switch (event.keycode) { case 13: //enter $(".next").click(); break; case 37: //prev arrow $(".prev").click(); break; case 39: //next arrow $(".next").click(); break; } }); });