原生js和jquery实现图片轮播特效

本文给大家分享的是使用原生JS和JQ两种方法分别实现相同的图片轮播特效,十分的实用,也非常方便大家对比学习原生js和jQuery,有需要的小伙伴可以参考下。

原生js和jquery实现图片轮播特效

1)首先是页面的结构部分

对于我这种左右切换式

1.首先是个外围部分(其实也就是最外边的整体wrapper)

2.接着就是你设置图片轮播的地方(也就是一个banner吧)

3.然后是一个图片组(可以用新的div 也可以直接使用 ul-->li形式)

4.然后是图片两端的左箭头和右箭头
5.然后是一个透明背景层,放在图片底部

6.然后是一个图片描述info层,放在透明背景层的左下角(div 或 ul-->li)

7.然后是一个按钮层,用来定位图片组的index吧,放在透明背景层的右下角(div 或 ul-->li)

由此,可以先构造出html结构

 1 <div id="wrapper"><!-- 最外层部分 -->
 2     <div id="banner"><!-- 轮播部分 -->
 3       <ul class="imgList"><!-- 图片部分 -->
 4       <li><a href="#"><img src="./img/test1.jpg" width="400px" height="200px" alt="puss in boots1"></a></li>
 5       <li><a href="#"><img src="./img/test2.jpg" width="400px" height="200px" alt="puss in boots2"></a></li>
 6       <li><a href="#"><img src="./img/test3.jpg" width="400px" height="200px" alt="puss in boots3"></a></li>
 7       <li><a href="#"><img src="./img/test4.jpg" width="400px" height="200px" alt="puss in boots4"></a></li>
 8       <li><a href="#"><img src="./img/test5.jpg" width="400px" height="200px" alt="puss in boots5"></a></li>
 9       </ul>
10       <img src="./img/prev.png" width="20px" height="40px" id="prev">
11       <img src="./img/next.png" width="20px" height="40px" id="next">
12       <div class="bg"></div> <!-- 图片底部背景层部分-->
13       <ul class="infoList"><!-- 图片左下角文字信息部分 -->
14         <li class="infoOn">puss in boots1</li>
15         <li>puss in boots2</li>
16         <li>puss in boots3</li>
17         <li>puss in boots4</li>
18         <li>puss in boots5</li>
19       </ul>
20       <ul class="indexList"><!-- 图片右下角序号部分 -->
21         <li class="indexOn">1</li>
22         <li>2</li>
23         <li>3</li>
24         <li>4</li>
25         <li>5</li>
26       </ul>
27     </div>

相对于之前,只是增多了两个箭头img标签

2)CSS样式部分(图片组的处理)跟淡入淡出式就不一样了

淡入淡出只需要显示或者隐藏对应序号的图片就行了,直接通过display来设定

左右切换式则是采用图片li 浮动,父层元素ul 总宽为总图片宽,并设定为有限banner宽度下隐藏超出宽度的部分

然后当想切换到某序号的图片时,则采用其ul 定位 left样式设定相应属性值实现

比如显示第一张图片初始定位left为0px, 要想显示第二张图片则需要left:-400px 处理

 1 <style type="text/css">
 2   body,div,ul,li,a,img{margin: 0;padding: 0;}
 3   ul,li{list-style: none;}
 4   a{text-decoration: none;}
 5  
 6   #wrapper{position: relative;margin: 30px auto;width: 400px;height: 200px;}
 7   #banner{position:relative;width: 400px;height: 200px;overflow: hidden;}
 8   .imgList{position:relative;width:2000px;height:200px;z-index: 10;overflow: hidden;}
 9   .imgList li{float:left;display: inline;}
10   #prev,
11   #next{position: absolute;top:80px;z-index: 20;cursor: pointer;opacity: 0.2;filter:alpha(opacity=20);}
12   #prev{left: 10px;}
13   #next{right: 10px;}
14   #prev:hover,
15   #next:hover{opacity: 0.5;filter:alpha(opacity=50);}
16   .bg{position: absolute;bottom: 0;width: 400px;height: 40px;z-index:20;opacity: 0.4;filter:alpha(opacity=40);background: black;}
17   .infoList{position: absolute;left: 10px;bottom: 10px;z-index: 30;}
18   .infoList li{display: none;}
19   .infoList .infoOn{display: inline;color: white;}
20   .indexList{position: absolute;right: 10px;bottom: 5px;z-index: 30;}
21   .indexList li{float: left;margin-right: 5px;padding: 2px 4px;border: 2px solid black;background: grey;cursor: pointer;}
22   .indexList .indexOn{background: red;font-weight: bold;color: white;}
23 </style>

(3)页面基本已经构建好久可以进行js的处理了

一、jQuery方式

照常先说jq处理

1.全局变量等

1 var curIndex = 0, //当前index
2      imgLen = $(".imgList li").length; //图片总数

2.自动切换定时器处理

 1 // 定时器自动变换2.5秒每次
 2 var autoChange = setInterval(function(){ 
 3   if(curIndex < imgLen-1){ 
 4     curIndex ++; 
 5   }else{ 
 6     curIndex = 0;
 7   }
 8   //调用变换处理函数
 9   changeTo(curIndex); 
10 },2500);

3.为左右箭头添加事件处理

左箭头

 1 //左箭头滑入滑出事件处理
 2 $("#prev").hover(function(){ 
 3   //滑入清除定时器
 4   clearInterval(autoChange);
 5 },function(){ 
 6   //滑出则重置定时器
 7   autoChangeAgain();
 8 });
 9 //左箭头点击处理
10 $("#prev").click(function(){ 
11   //根据curIndex进行上一个图片处理
12   curIndex = (curIndex > 0) ? (--curIndex) : (imgLen - 1);
13   changeTo(curIndex);
14 });

右箭头

 1 //右箭头滑入滑出事件处理
 2  $("#next").hover(function(){ 
 3    //滑入清除定时器
 4    clearInterval(autoChange);
 5  },function(){ 
 6    //滑出则重置定时器
 7    autoChangeAgain();
 8  });
 9  //右箭头点击处理
10  $("#next").click(function(){ 
11    curIndex = (curIndex < imgLen - 1) ? (++curIndex) : 0;
12    changeTo(curIndex);
13  });

其中autoChangeAgain()就是一个重置定时器函数

 1 //清除定时器时候的重置定时器--封装
 2   function autoChangeAgain(){ 
 3       autoChange = setInterval(function(){ 
 4       if(curIndex < imgLen-1){ 
 5         curIndex ++;
 6       }else{ 
 7         curIndex = 0;
 8       }
 9     //调用变换处理函数
10       changeTo(curIndex); 
11     },2500);
12     }

其中changeTo()就是一个图片切换的处理函数

1 function changeTo(num){ 
2     var goLeft = num * 400;
3     $(".imgList").animate({left: "-" + goLeft + "px"},500);
4     $(".infoList").find("li").removeClass("infoOn").eq(num).addClass("infoOn");
5     $(".indexList").find("li").removeClass("indexOn").eq(num).addClass("indexOn");
6   }

每传入一个图片序号,则按理进行goLeft

4.为右下角的那几个li 按钮绑定事件处理

 1 //对右下角按钮index进行事件绑定处理等
 2   $(".indexList").find("li").each(function(item){ 
 3     $(this).hover(function(){ 
 4       clearInterval(autoChange);
 5       changeTo(item);
 6       curIndex = item;
 7     },function(){ 
 8       autoChangeAgain();
 9     });
10   });

jq就是这样,简便,原生代码量就有些多了

完整代码

  1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2 <html xmlns="http://www.w3.org/1999/xhtml">
  3 <head>
  4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5 <title>图片轮播 jq(左右切换)</title>
  6 <style type="text/css">
  7   body,div,ul,li,a,img{margin: 0;padding: 0;}
  8   ul,li{list-style: none;}
  9   a{text-decoration: none;}
 10   #wrapper{position: relative;margin: 30px auto;width: 400px;height: 200px;}
 11   #banner{position:relative;width: 400px;height: 200px;overflow: hidden;}
 12   .imgList{position:relative;width:2000px;height:200px;z-index: 10;overflow: hidden;}
 13   .imgList li{float:left;display: inline;}
 14   #prev,
 15   #next{position: absolute;top:80px;z-index: 20;cursor: pointer;opacity: 0.2;filter:alpha(opacity=20);}
 16   #prev{left: 10px;}
 17   #next{right: 10px;}
 18   #prev:hover,
 19   #next:hover{opacity: 0.5;filter:alpha(opacity=50);}
 20   .bg{position: absolute;bottom: 0;width: 400px;height: 40px;z-index:20;opacity: 0.4;filter:alpha(opacity=40);background: black;}
 21   .infoList{position: absolute;left: 10px;bottom: 10px;z-index: 30;}
 22   .infoList li{display: none;}
 23   .infoList .infoOn{display: inline;color: white;}
 24   .indexList{position: absolute;right: 10px;bottom: 5px;z-index: 30;}
 25   .indexList li{float: left;margin-right: 5px;padding: 2px 4px;border: 2px solid black;background: grey;cursor: pointer;}
 26   .indexList .indexOn{background: red;font-weight: bold;color: white;}
 27 </style>
 28 </head>
 29 <body>
 30   <div id="wrapper"><!-- 最外层部分 -->
 31     <div id="banner"><!-- 轮播部分 -->
 32       <ul class="imgList"><!-- 图片部分 -->
 33         <li><a href="#"><img src="./img/test1.jpg" width="400px" height="200px" alt="puss in boots1"></a></li>
 34       <li><a href="#"><img src="./img/test2.jpg" width="400px" height="200px" alt="puss in boots2"></a></li>
 35       <li><a href="#"><img src="./img/test3.jpg" width="400px" height="200px" alt="puss in boots3"></a></li>
 36       <li><a href="#"><img src="./img/test4.jpg" width="400px" height="200px" alt="puss in boots4"></a></li>
 37       <li><a href="#"><img src="./img/test5.jpg" width="400px" height="200px" alt="puss in boots5"></a></li>
 38       </ul>
 39       <img src="./img/prev.png" width="20px" height="40px" id="prev">
 40       <img src="./img/next.png" width="20px" height="40px" id="next">
 41       <div class="bg"></div> <!-- 图片底部背景层部分-->
 42       <ul class="infoList"><!-- 图片左下角文字信息部分 -->
 43         <li class="infoOn">puss in boots1</li>
 44         <li>puss in boots2</li>
 45         <li>puss in boots3</li>
 46         <li>puss in boots4</li>
 47         <li>puss in boots5</li>
 48       </ul>
 49       <ul class="indexList"><!-- 图片右下角序号部分 -->
 50         <li class="indexOn">1</li>
 51         <li>2</li>
 52         <li>3</li>
 53         <li>4</li>
 54         <li>5</li>
 55       </ul>
 56     </div>
 57   </div>
 58   <script type="text/javascript" src="./js/jquery.min.js"></script>
 59   <script type="text/javascript">
 60   var curIndex = 0, //当前index
 61       imgLen = $(".imgList li").length; //图片总数
 62      // 定时器自动变换2.5秒每次
 63   var autoChange = setInterval(function(){ 
 64     if(curIndex < imgLen-1){ 
 65       curIndex ++; 
 66     }else{ 
 67       curIndex = 0;
 68     }
 69     //调用变换处理函数
 70     changeTo(curIndex); 
 71   },2500);
 72    //左箭头滑入滑出事件处理
 73   $("#prev").hover(function(){ 
 74     //滑入清除定时器
 75     clearInterval(autoChange);
 76   },function(){ 
 77     //滑出则重置定时器
 78     autoChangeAgain();
 79   });
 80   //左箭头点击处理
 81   $("#prev").click(function(){ 
 82     //根据curIndex进行上一个图片处理
 83     curIndex = (curIndex > 0) ? (--curIndex) : (imgLen - 1);
 84     changeTo(curIndex);
 85   });
 86   //右箭头滑入滑出事件处理
 87   $("#next").hover(function(){ 
 88     //滑入清除定时器
 89     clearInterval(autoChange);
 90   },function(){ 
 91     //滑出则重置定时器
 92     autoChangeAgain();
 93   });
 94   //右箭头点击处理
 95   $("#next").click(function(){ 
 96     curIndex = (curIndex < imgLen - 1) ? (++curIndex) : 0;
 97     changeTo(curIndex);
 98   });
 99   //对右下角按钮index进行事件绑定处理等
100   $(".indexList").find("li").each(function(item){ 
101     $(this).hover(function(){ 
102       clearInterval(autoChange);
103       changeTo(item);
104       curIndex = item;
105     },function(){ 
106       autoChangeAgain();
107     });
108   });
109   //清除定时器时候的重置定时器--封装
110   function autoChangeAgain(){ 
111       autoChange = setInterval(function(){ 
112       if(curIndex < imgLen-1){ 
113         curIndex ++;
114       }else{ 
115         curIndex = 0;
116       }
117     //调用变换处理函数
118       changeTo(curIndex); 
119     },2500);
120     }
121   function changeTo(num){ 
122     var goLeft = num * 400;
123     $(".imgList").animate({left: "-" + goLeft + "px"},500);
124     $(".infoList").find("li").removeClass("infoOn").eq(num).addClass("infoOn");
125     $(".indexList").find("li").removeClass("indexOn").eq(num).addClass("indexOn");
126   }
127   </script>
128 </body>
129 </html>

二、js 原生实现

js原生大概也就是模拟jq的实现思路

1.全局变量等

1 var curIndex = 0, //当前index
2       imgArr = getElementsByClassName("imgList")[0].getElementsByTagName("li"), //获取图片组
3       imgLen = imgArr.length,
4       infoArr = getElementsByClassName("infoList")[0].getElementsByTagName("li"), //获取图片info组
5       indexArr = getElementsByClassName("indexList")[0].getElementsByTagName("li"); //获取控制index组

2.自动切换定时器处理

 1   // 定时器自动变换2.5秒每次
 2 var autoChange = setInterval(function(){ 
 3   if(curIndex < imgLen -1){ 
 4     curIndex ++; 
 5   }else{ 
 6     curIndex = 0;
 7   }
 8   //调用变换处理函数
 9   changeTo(curIndex); 
10 },2500);

同样的,有一个重置定时器的函数

 1 //清除定时器时候的重置定时器--封装
 2  function autoChangeAgain(){ 
 3      autoChange = setInterval(function(){ 
 4      if(curIndex < imgLen -1){ 
 5        curIndex ++;
 6      }else{ 
 7        curIndex = 0;
 8      }
 9    //调用变换处理函数
10      changeTo(curIndex); 
11    },2500);
12    }

3.因为有一些class呀,所以来几个class函数的模拟也是需要的

 1 //通过class获取节点
 2  function getElementsByClassName(className){ 
 3    var classArr = [];
 4    var tags = document.getElementsByTagName('*');
 5    for(var item in tags){ 
 6      if(tags[item].nodeType == 1){ 
 7        if(tags[item].getAttribute('class') == className){ 
 8          classArr.push(tags[item]);
 9        }
10      }
11    }
12    return classArr; //返回
13  }
14  
15  // 判断obj是否有此class
16  function hasClass(obj,cls){  //class位于单词边界
17    return obj.className.match(new RegExp('(\s|^)' + cls + '(\s|$)'));
18   }
19   //给 obj添加class
20  function addClass(obj,cls){ 
21    if(!this.hasClass(obj,cls)){ 
22       obj.className += cls;
23    }
24  }
25  //移除obj对应的class
26  function removeClass(obj,cls){ 
27    if(hasClass(obj,cls)){ 
28      var reg = new RegExp('(\s|^)' + cls + '(\s|$)');
29         obj.className = obj.className.replace(reg,'');
30    }
31  }

4.要左右切换,就得模拟jq的animate-->left .

我的思路就是动态地设置element.style.left 进行定位。因为要有一个渐进的过程,所以加上的一点点阶段处理。

定位的时候left的设置也是有点复杂的..要考虑方向等情况

 1 //图片组相对原始左移dist px距离
 2  function goLeft(elem,dist){ 
 3    if(dist == 400){ //第一次时设置left为0px 或者直接使用内嵌法 style="left:0;"
 4      elem.style.left = "0px";
 5    }
 6    var toLeft; //判断图片移动方向是否为左
 7    dist = dist + parseInt(elem.style.left); //图片组相对当前移动距离
 8    if(dist<0){  
 9      toLeft = false;
10      dist = Math.abs(dist);
11    }else{ 
12      toLeft = true;
13    }
14    for(var i=0;i<= dist/20;i++){ //这里设定缓慢移动,10阶每阶40px
15      (function(_i){ 
16        var pos = parseInt(elem.style.left); //获取当前left
17        setTimeout(function(){ 
18          pos += (toLeft)? -(_i * 20) : (_i * 20); //根据toLeft值指定图片组位置改变
19          //console.log(pos);
20          elem.style.left = pos + "px";
21        },_i * 25); //每阶间隔50毫秒
22      })(i);
23    }
24  }

上头也看到了,我初始了left的值为0px

我试过了,如果不初始或者把初始的left值写在行内css样式表里边,就总会报错取不到

所以直接在js中初始化或者在html中内嵌初始化也可。

5.接下来就是切换的函数实现了,比如要切换到序号为num的图片

 1 //左右切换处理函数
 2   function changeTo(num){ 
 3     //设置image
 4     var imgList = getElementsByClassName("imgList")[0];
 5     goLeft(imgList,num*400); //左移一定距离
 6     //设置image 的 info
 7     var curInfo = getElementsByClassName("infoOn")[0];
 8     removeClass(curInfo,"infoOn");
 9     addClass(infoArr[num],"infoOn");
10     //设置image的控制下标 index
11     var _curIndex = getElementsByClassName("indexOn")[0];
12     removeClass(_curIndex,"indexOn");
13     addClass(indexArr[num],"indexOn");
14   }

6.然后再给左右箭头还有右下角那堆index绑定事件处理

 1 //给左右箭头和右下角的图片index添加事件处理
 2  function addEvent(){
 3   for(var i=0;i<imgLen;i++){ 
 4     //闭包防止作用域内活动对象item的影响
 5     (function(_i){ 
 6     //鼠标滑过则清除定时器,并作变换处理
 7     indexArr[_i].onmouseover = function(){ 
 8       clearTimeout(autoChange);
 9       changeTo(_i);
10       curIndex = _i;
11     };
12     //鼠标滑出则重置定时器处理
13     indexArr[_i].onmouseout = function(){ 
14       autoChangeAgain();
15     };
16      })(i);
17   }
18  
19   //给左箭头prev添加上一个事件
20   var prev = document.getElementById("prev");
21   prev.onmouseover = function(){ 
22     //滑入清除定时器
23     clearInterval(autoChange);
24   };
25   prev.onclick = function(){ 
26     //根据curIndex进行上一个图片处理
27     curIndex = (curIndex > 0) ? (--curIndex) : (imgLen - 1);
28     changeTo(curIndex);
29   };
30   prev.onmouseout = function(){ 
31     //滑出则重置定时器
32     autoChangeAgain();
33   };
34  
35    //给右箭头next添加下一个事件
36   var next = document.getElementById("next");
37   next.onmouseover = function(){ 
38     clearInterval(autoChange);
39   };
40   next.onclick = function(){ 
41     curIndex = (curIndex < imgLen - 1) ? (++curIndex) : 0;
42     changeTo(curIndex);
43   };
44   next.onmouseout = function(){ 
45     autoChangeAgain();
46   };
47 }

7.最后的最后,没啥了. 噢好像还要调用一下下那个 addEvent() ..

完整代码  代码量有些冗杂..

  1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2 <html xmlns="http://www.w3.org/1999/xhtml">
  3 <head>
  4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5 <title>图片轮播 js原生(左右切换)</title>
  6 <style type="text/css">
  7   body,div,ul,li,a,img{margin: 0;padding: 0;}
  8   ul,li{list-style: none;}
  9   a{text-decoration: none;}
 10  
 11   #wrapper{position: relative;margin: 30px auto;width: 400px;height: 200px;}
 12   #banner{position:relative;width: 400px;height: 200px;overflow: hidden;}
 13   .imgList{position:relative;width:2000px;height:200px;z-index: 10;overflow: hidden;}
 14   .imgList li{float:left;display: inline;}
 15   #prev,
 16   #next{position: absolute;top:80px;z-index: 20;cursor: pointer;opacity: 0.2;filter:alpha(opacity=20);}
 17   #prev{left: 10px;}
 18   #next{right: 10px;}
 19   #prev:hover,
 20   #next:hover{opacity: 0.5;filter:alpha(opacity=50);}
 21   .bg{position: absolute;bottom: 0;width: 400px;height: 40px;z-index:20;opacity: 0.4;filter:alpha(opacity=40);background: black;}
 22   .infoList{position: absolute;left: 10px;bottom: 10px;z-index: 30;}
 23   .infoList li{display: none;}
 24   .infoList .infoOn{display: inline;color: white;}
 25   .indexList{position: absolute;right: 10px;bottom: 5px;z-index: 30;}
 26   .indexList li{float: left;margin-right: 5px;padding: 2px 4px;border: 2px solid black;background: grey;cursor: pointer;}
 27   .indexList .indexOn{background: red;font-weight: bold;color: white;}
 28 </style>
 29 </head>
 30 <body>
 31   <div id="wrapper"><!-- 最外层部分 -->
 32     <div id="banner"><!-- 轮播部分 -->
 33       <ul class="imgList"><!-- 图片部分 -->
 34         <li><a href="#"><img src="./img/test1.jpg" width="400px" height="200px" alt="puss in boots1"></a></li>
 35       <li><a href="#"><img src="./img/test2.jpg" width="400px" height="200px" alt="puss in boots2"></a></li>
 36       <li><a href="#"><img src="./img/test3.jpg" width="400px" height="200px" alt="puss in boots3"></a></li>
 37       <li><a href="#"><img src="./img/test4.jpg" width="400px" height="200px" alt="puss in boots4"></a></li>
 38       <li><a href="#"><img src="./img/test5.jpg" width="400px" height="200px" alt="puss in boots5"></a></li>
 39       </ul>
 40       <img src="./img/prev.png" width="20px" height="40px" id="prev">
 41       <img src="./img/next.png" width="20px" height="40px" id="next">
 42       <div class="bg"></div> <!-- 图片底部背景层部分-->
 43       <ul class="infoList"><!-- 图片左下角文字信息部分 -->
 44         <li class="infoOn">puss in boots1</li>
 45         <li>puss in boots2</li>
 46         <li>puss in boots3</li>
 47         <li>puss in boots4</li>
 48         <li>puss in boots5</li>
 49       </ul>
 50       <ul class="indexList"><!-- 图片右下角序号部分 -->
 51         <li class="indexOn">1</li>
 52         <li>2</li>
 53         <li>3</li>
 54         <li>4</li>
 55         <li>5</li>
 56       </ul>
 57     </div>
 58   </div>
 59   <script type="text/javascript">
 60   var curIndex = 0, //当前index
 61       imgArr = getElementsByClassName("imgList")[0].getElementsByTagName("li"), //获取图片组
 62       imgLen = imgArr.length,
 63       infoArr = getElementsByClassName("infoList")[0].getElementsByTagName("li"), //获取图片info组
 64       indexArr = getElementsByClassName("indexList")[0].getElementsByTagName("li"); //获取控制index组
 65      // 定时器自动变换2.5秒每次
 66   var autoChange = setInterval(function(){ 
 67     if(curIndex < imgLen -1){ 
 68       curIndex ++; 
 69     }else{ 
 70       curIndex = 0;
 71     }
 72     //调用变换处理函数
 73     changeTo(curIndex); 
 74   },2500);
 75  
 76   //清除定时器时候的重置定时器--封装
 77   function autoChangeAgain(){ 
 78       autoChange = setInterval(function(){ 
 79       if(curIndex < imgLen -1){ 
 80         curIndex ++;
 81       }else{ 
 82         curIndex = 0;
 83       }
 84     //调用变换处理函数
 85       changeTo(curIndex); 
 86     },2500);
 87     }
 88  
 89   //调用添加事件处理
 90   addEvent();
 91  
 92   //给左右箭头和右下角的图片index添加事件处理
 93  function addEvent(){
 94   for(var i=0;i<imgLen;i++){ 
 95     //闭包防止作用域内活动对象item的影响
 96     (function(_i){ 
 97     //鼠标滑过则清除定时器,并作变换处理
 98     indexArr[_i].onmouseover = function(){ 
 99       clearTimeout(autoChange);
100       changeTo(_i);
101       curIndex = _i;
102     };
103     //鼠标滑出则重置定时器处理
104     indexArr[_i].onmouseout = function(){ 
105       autoChangeAgain();
106     };
107      })(i);
108   }
109  
110   //给左箭头prev添加上一个事件
111   var prev = document.getElementById("prev");
112   prev.onmouseover = function(){ 
113     //滑入清除定时器
114     clearInterval(autoChange);
115   };
116   prev.onclick = function(){ 
117     //根据curIndex进行上一个图片处理
118     curIndex = (curIndex > 0) ? (--curIndex) : (imgLen - 1);
119     changeTo(curIndex);
120   };
121   prev.onmouseout = function(){ 
122     //滑出则重置定时器
123     autoChangeAgain();
124   };
125  
126    //给右箭头next添加下一个事件
127   var next = document.getElementById("next");
128   next.onmouseover = function(){ 
129     clearInterval(autoChange);
130   };
131   next.onclick = function(){ 
132     curIndex = (curIndex < imgLen - 1) ? (++curIndex) : 0;
133     changeTo(curIndex);
134   };
135   next.onmouseout = function(){ 
136     autoChangeAgain();
137   };
138 }
139  
140   //左右切换处理函数
141   function changeTo(num){ 
142     //设置image
143     var imgList = getElementsByClassName("imgList")[0];
144     goLeft(imgList,num*400); //左移一定距离
145     //设置image 的 info
146     var curInfo = getElementsByClassName("infoOn")[0];
147     removeClass(curInfo,"infoOn");
148     addClass(infoArr[num],"infoOn");
149     //设置image的控制下标 index
150     var _curIndex = getElementsByClassName("indexOn")[0];
151     removeClass(_curIndex,"indexOn");
152     addClass(indexArr[num],"indexOn");
153   }
154  
155  
156   //图片组相对原始左移dist px距离
157   function goLeft(elem,dist){ 
158     if(dist == 400){ //第一次时设置left为0px 或者直接使用内嵌法 style="left:0;"
159       elem.style.left = "0px";
160     }
161     var toLeft; //判断图片移动方向是否为左
162     dist = dist + parseInt(elem.style.left); //图片组相对当前移动距离
163     if(dist<0){  
164       toLeft = false;
165       dist = Math.abs(dist);
166     }else{ 
167       toLeft = true;
168     }
169     for(var i=0;i<= dist/20;i++){ //这里设定缓慢移动,10阶每阶40px
170       (function(_i){ 
171         var pos = parseInt(elem.style.left); //获取当前left
172         setTimeout(function(){ 
173           pos += (toLeft)? -(_i * 20) : (_i * 20); //根据toLeft值指定图片组位置改变
174           //console.log(pos);
175           elem.style.left = pos + "px";
176         },_i * 25); //每阶间隔50毫秒
177       })(i);
178     }
179   }
180  
181   //通过class获取节点
182   function getElementsByClassName(className){ 
183     var classArr = [];
184     var tags = document.getElementsByTagName('*');
185     for(var item in tags){ 
186       if(tags[item].nodeType == 1){ 
187         if(tags[item].getAttribute('class') == className){ 
188           classArr.push(tags[item]);
189         }
190       }
191     }
192     return classArr; //返回
193   }
194  
195   // 判断obj是否有此class
196   function hasClass(obj,cls){  //class位于单词边界
197     return obj.className.match(new RegExp('(\s|^)' + cls + '(\s|$)'));
198    }
199    //给 obj添加class
200   function addClass(obj,cls){ 
201     if(!this.hasClass(obj,cls)){ 
202        obj.className += cls;
203     }
204   }
205   //移除obj对应的class
206   function removeClass(obj,cls){ 
207     if(hasClass(obj,cls)){ 
208       var reg = new RegExp('(\s|^)' + cls + '(\s|$)');
209          obj.className = obj.className.replace(reg,'');
210     }
211   }
212  
213   </script>
214 </body>
215 </html>