写的一个轮播插件 效果图如下:

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>test </title> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> <style type="text/css"> *{margin:0;padding:0;} ul,li,ol{ list-style:none;} img,a img{ border:none;} table{ border-collapse:collapse; border-spacing:0;} em,i,u{ font-style:normal;} a{ text-decoration:none;color:#434343;} a:hover{ text-decoration:underline; color:#ff4000;} body{font-size:12px; font-family:"宋体";color:#434343;} .clear{clear:both; font-size:0; overflow:hidden; height:0;} .blank10{clear:both; font-size:0; overflow:hidden; height:10px;} .blank{clear:both; font-size:0; overflow:hidden; height:10px; background:#FFF;} /*********content css定义***********/ #content{width:960px;margin:0 auto;} h1,h2,h3{ font-family:"微软雅黑";color:#00659b;} h1,h2,h3,h4,h5,h6{ font-weight:bold;} #footer{padding-bottom:0;margin:0px auto 0;} /*********main css定义***********/ .mb15 {margin-bottom: 0px;} #main{width:960px; margin:6px auto 10px; overflow:hidden;} .sub_box{width:663px; height:300px; float:left; position:relative; overflow:hidden; border:1px solid #cccccc; background:url(http://trip.elong.com/trip/default/news/images/logo_gray.jpg) center center no-repeat; *background:url(http://trip.elong.com/trip/default/news/images/logo_gray.jpg) 220px 120px no-repeat;} .sub_box img{border:none; width:663px; height:300px;} .sub_nav{width:663px; height:25px; bottom:0px; position:absolute; color:#999999; z-index:200;} .sub_no{height:25px; float:right;} .sub_no li{display:block; width:25px; height:25px; float:left; overflow:hidden; line-height:25px; vertical-align:middle; text-align:center; background:#000000; filter:alpha(Opacity=70); Opacity:0.7; font-family:Arial; font-size:11px; color:#999999; cursor:pointer;} .sub_no li.show{background:#ffffff; color:#000000;} .sub_more{float:right; width:50px; height:25px; background:#000000; filter:alpha(Opacity=70); Opacity:0.7; color:#999999; text-align:center; line-height:25px;} .sub_more a{color:#999999;} .sub_more a:hover{color:#cccccc; text-decoration:none;} </style> <script type="text/javascript"> function $(id,tag){ var re=(id&&typeof id!="string")?id:document.getElementById(id); if(!tag){ return re; } else{ return re.getElementsByTagName(tag); } } function move(moveWidth,pageItems,displayEl){ var currentMoving=false; var carouselInterval; var changePageTimeout; var currentShowIndex=0; var currentHideIndex=1; var pShowEl=$(pageItems[currentShowIndex].itemId).parentNode; var initPageItemEvent=function(){ var len= pageItems.length; for(var i=0;i<len;i++){ $(pageItems[i].btn).setAttribute("pageIndex",i); //按钮事件添加 可以做成配置项 添加自定义点击事件 此处没有处理 $(pageItems[i].btn).onclick= function(){ var cIndex=this.getAttribute("pageIndex")-0; if(currentMoving || currentShowIndex==cIndex){ return false; } clearTimeout(changePageTimeout); currentHideIndex=cIndex; if(currentShowIndex>cIndex){ changePaging(true); }else{ changePaging(false); } } } }; var changePosition=function(toRight){ var currentItem=$(pageItems[currentShowIndex].itemId); var hidePage=$(pageItems[currentHideIndex].itemId); var hideLeft=hidePage.style.left; hideLeft=parseInt(hideLeft+0,10); var cWidth=Math.ceil(moveWidth/20); if(toRight){ var hideMwidth=hideLeft+cWidth; var currMwidth=hideLeft+moveWidth; }else{ var hideMwidth=hideLeft-cWidth; var currMwidth=hideLeft-moveWidth; } if((hideMwidth<=0 && !toRight) || (toRight && hideMwidth>=0)){ hideMwidth=0; clearInterval(carouselInterval); setTimeout(function(){ var currentItem=$(pageItems[currentShowIndex].itemId); currentItem.style.display="none"; currentItem.style.left=moveWidth+"px"; displayEl.appendChild(currentItem); currentShowIndex=currentHideIndex; currentMoving=false; },0); changePageTimeout=setTimeout(function(){ currentHideIndex++; if(!pageItems[currentHideIndex]){ currentHideIndex=0; } changePaging(); },5000); } //我测试用的left currentItem.style.left=currMwidth+"px"; hidePage.style.left =hideMwidth+"px"; }; var changePaging= function(toRight){ currentMoving=true; $(pageItems[currentShowIndex].btn).className=""; $(pageItems[currentHideIndex].btn).className="show"; var hideItem=$(pageItems[currentHideIndex].itemId); var hideItemLeft=moveWidth; if(toRight){ hideItemLeft=0-moveWidth; pShowEl.insertBefore(hideItem,$(pageItems[currentShowIndex].itemId)); }else{ pShowEl.insertBefore(hideItem,$(pageItems[currentShowIndex].itemId).nextSibling); } hideItem.style.left=hideItemLeft+"px"; hideItem.style.display=""; carouselInterval=setInterval(function(){changePosition(toRight);},100); }; changePageTimeout=setTimeout(changePaging,5000); initPageItemEvent(); } window.onload=function(){ //每次轮播移动的距离,每次移动按moveWidth./20计算 var moveWidth=663; //轮播的数组 按钮和item dom对象的id 从0开始播放 //对没有按钮的情况没有去兼容 //每次过5秒换图。多久播放下一幅图片没有做成配置选项 如修改可做成配置 也可直接替换5000 var pageItems=[ { btn:"btnPage1", itemId:"pageNum1" },{ btn:"btnPage2", itemId:"pageNum2" },{ btn:"btnPage3", itemId:"pageNum3" },{ btn:"btnPage4", itemId:"pageNum4" },{ btn:"btnPage5", itemId:"pageNum5" } ]; //调用接口播放 move(moveWidth,pageItems,$("displayDiv")); }; </script> </head> <body > <div class="sub_box"> <div id="p-select" class="sub_nav"> <div class="sub_no" id="appNum"> <ul > <li class="show" id="btnPage1">1</li> <li class="" id="btnPage2">2</li> <li class="" id="btnPage3">3</li> <li class="" id="btnPage4">4</li> <li class="" id="btnPage5">5</li> </ul> </div> </div> <div id="show" style="position:relative;"> <div style="left:0px;position:absolute;" id="pageNum1"> <img src="1.png"> </div> </div> </div> <div id="displayDiv"> <div style="left:663px;position:absolute;display:none;" id="pageNum2"> <img src="2.png"> </div> <div style="left:663px;position:absolute;display:none;" id="pageNum3"> <img src="3.png"> </div> <div style="left:663px;position:absolute;display:none;" id="pageNum4"> <img src="4.png"> </div> <div style="left:663px;position:absolute;display:none;" id="pageNum5"> <img src="5.png"> </div> <div> </body> </html>