JavaScript旋转木马轮播图
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>旋转木马轮播图</title> <link rel="stylesheet" href="css/css.css"/> <script src="common.js"></script> <script> // var config = [ { width: 400, top: 20, left: 50, opacity: 0.2, zIndex: 2 },//0 { width: 600, top: 70, left: 0, opacity: 0.8, zIndex: 3 },//1 { width: 800, top: 100, left: 200, opacity: 1, zIndex: 4 },//2 { width: 600, top: 70, left: 600, opacity: 0.8, zIndex: 3 },//3 { width: 400, top: 20, left: 750, opacity: 0.2, zIndex: 2 }//4 ]; //页面加载的事件 window.onload = function () { var flag = true;//假设所有的动画都执行完毕 //获取所有的li标签循环遍历设置样式 var list = my$("slide").getElementsByTagName("li"); function assign() { for (var i = 0; i < list.length; i++) { animate(list[i], config[i], function () { flag = true; }); } } assign(); //鼠标进入跟鼠标离开事件 my$("wrap").onmouseover = function () { animate(my$("arrow"), {"opacity": 1}); }; my$("wrap").onmouseout = function () { animate(my$("arrow"), {"opacity": 0}) }; //右边按钮 my$("arrRight").onclick = function () { if (flag) { flag = false; config.push(config.shift());//把数组的第一个元素放到最后一个 assign();//重新分配 } }; //左边按钮 my$("arrLeft").onclick = function () { if (flag) { flag = false; config.unshift(config.pop());//吧数组中的最后一个元素放到第一个 assign();//重新分配 } } }; </script> </head> <body> <div class="wrap" id="wrap"> <div class="slide" id="slide"> <ul> <li><a href="#"><img src="images/slidepic1.jpg" alt=""/></a></li> <li><a href="#"><img src="images/slidepic2.jpg" alt=""/></a></li> <li><a href="#"><img src="images/slidepic3.jpg" alt=""/></a></li> <li><a href="#"><img src="images/slidepic4.jpg" alt=""/></a></li> <li><a href="#"><img src="images/slidepic5.jpg" alt=""/></a></li> </ul> <div class="arrow" id="arrow"> <a href="javascript:;" class="prev" id="arrLeft"></a> <a href="javascript:;" class="next" id="arrRight"></a> </div> </div> </div> </body> </html>
css.css
@charset "UTF-8"; /*初始化 reset*/ blockquote,body,button,dd,dl,dt,fieldset,form,h1,h2,h3,h4,h5,h6,hr,input,legend,li,ol,p,pre,td,textarea,th,ul{margin:0;padding:0} body,button,input,select,textarea{font:12px/1.5 "Microsoft YaHei", "微软雅黑", SimSun, "宋体", sans-serif;color: #666;} ol,ul{list-style:none} a{text-decoration:none} fieldset,img{border:0;vertical-align:top;} a,input,button,select,textarea{outline:none;} a,button{cursor:pointer;} .wrap{ width:1200px; margin:100px auto; } .slide { height:500px; position: relative; } .slide li{ position: absolute; left:200px; top:0; } .slide li img{ width:100%; } .arrow{ opacity: 0; } .prev,.next{ width:76px; height:112px; position: absolute; top:50%; margin-top:-56px; background: url(../images/prev.png) no-repeat; z-index: 99; } .next{ right:0; background-image: url(../images/next.png); }
common.js
//根据id获取对应的元素 function my$(id) { return document.getElementById(id); } /* * element---任意的元素 * attr---属性 * */ function getAttrValue(element,attr) { return element.currentStyle?element.currentStyle[attr] : window.getComputedStyle(element,null)[attr]||0; } /* * element----要移动的元素 * target----移动的目标 * 初级版本 * */ /* * 终极版本的动画函数---有bug * * */ function animate(element,json,fn) { clearInterval(element.timeId); element.timeId=setInterval(function () { var flag=true;//假设都达到了目标 for(var attr in json){ if(attr=="opacity"){//判断属性是不是opacity var current= getAttrValue(element,attr)*100; //每次移动多少步 var target=json[attr]*100;//直接赋值给一个变量,后面的代码都不用改 var step=(target-current)/10;//(目标-当前)/10 step=step>0?Math.ceil(step):Math.floor(step); current=current+step; element.style[attr]=current/100; }else if(attr=="zIndex"){//判断属性是不是zIndex element.style[attr]=json[attr]; }else{//普通的属性 //获取当前的位置----getAttrValue(element,attr)获取的是字符串类型 var current= parseInt(getAttrValue(element,attr))||0; //每次移动多少步 var target=json[attr];//直接赋值给一个变量,后面的代码都不用改 var step=(target-current)/10;//(目标-当前)/10 step=step>0?Math.ceil(step):Math.floor(step); current=current+step; element.style[attr]=current+"px"; } if(current!=target){ flag=false;//如果没到目标结果就为false } } if(flag){//结果为true clearInterval(element.timeId); if(fn){//如果用户传入了回调的函数 fn(); //就直接的调用, } } console.log("target:"+target+"current:"+current+"step:"+step); },10); }
用到的图片