JS:常见多张图片切换、图片轮播、自动切换效果代码
(2017-02-17 15:57:01)效果如图所示:
设计思路:
1、设计好图片轮播HTML代码,以及CSS定位,CSS中着重运用好position属性,relative和absolute;
CSS代码如下:
1 <style type="text/css"> 2 *{margin:0; padding:0;} 3 ul{list-style-type:none;} 4 body{ color:#333333; font:12px/1.6em Arial, Helvetica, sans-serif;} 5 #picfocus{width:345px; height:235px; border:1px solid #ccc; margin:5px;} 6 #piclist{position: relative; float:left; width:240px; height:225px; overflow:hidden; margin:5px 0px 0px 9px; display:inline;} 7 #pic{position:absolute;} 8 #pic li{overflow:hidden;width:240px; height:225px;} 9 #pic img{ width:240px; height:225px;} 10 #pictxtbg{position:absolute; bottom:0px; left:0px; background: none repeat scroll 0 0 #000000; opacity:0.5; width:240px; height:35px; filter:alpha(opacity=50);} 11 #pictxt{ position:absolute; bottom:8px; left:8px; color:#fff;} 12 #pictxt .normal{display:none;} 13 #pictxt .current{ display:block;} 14 #pic{ overflow:hidden;} 15 #picbtn{float:right; display:inline; margin:2px 9px 0px 0px;} 16 #picbtn li{ cursor:pointer; height:57px; opacity:0.5; filter:alpha(opacity=50);} 17 #picbtn img{ width:75px; height:45px; margin:7px 0px 0px 11px;} 18 #picbtn .current{ opacity:1; filter:alpha(opacity=100);} 19 </style>
2、JS代码设计思路:
(1)当鼠标移动到小图片按钮上时,促发onmouseover函数,调整大图片显示的当前位置,显示当前对应的大图片,以及改变当前显示的小图片按钮及图片标题样式。
(2)编写移动相应的大图片到当前位置的函数,movePic(pic,final_x,final_y,interval);
编写初始化小图片按钮的样式及标题样式,classNormal(picbtn,pictxt)
编写显示当前图片的样式及标题样式,classCurrent(picbtn,pictxt,n)
(3)当鼠标移动到小图片按钮时,调用以上三个函数。
(4)自动切换图片,编写函数autoChange(),利用setInterval('autoChange()',interval)函数自动切换图片。
相关js代码为:
1 <script type="text/javascript"> 2 //加载window.onload 3 function addEventLoad(func){ 4 var oldonload = window.onload; 5 if(typeof(window.onload) != 'funciton'){ 6 window.onload = func; 7 } 8 else { 9 window.onload = function(){ 10 oldonload(); 11 func(); 12 } 13 } 14 } 15 //初始化小图片及图片标题样式 16 function classNormal(picbtn,pictxt){ 17 var picbtns = document.getElementById(picbtn).getElementsByTagName("li"); 18 var pictxts = document.getElementById(pictxt).getElementsByTagName("li"); 19 for(var i = 0; i < picbtns.length; i++){ 20 picbtns[i].className = "normal"; 21 pictxts[i].className = "normal"; 22 } 23 } 24 //当前显示的小图片及图片标题样式 25 function classCurrent(picbtn,pictxt,n){ 26 var picbtns = document.getElementById(picbtn).getElementsByTagNam("li"); 27 var pictxts = document.getElementById(pictxt).getElementsByTagName("li"); 28 picbtns[n].className = "current"; 29 pictxts[n].className = "current"; 30 } 31 //移动图片 32 function movePic(pic,final_x,final_y,interval){ 33 var elem = document.getElementBy(pic); 34 var xpos = parseInt(elem.style.left); 35 var ypos = parseInt(elem.style.top); 36 if(elem.movement){ 37 clearTimeout(elem.movement); 38 } 39 if (!elem.style.left) { 40 elem.style.left = "0px"; 41 } 42 if (!elem.style.top) { 43 elem.style.top = "0px"; 44 } 45 if (xpos == final_x && ypos == final_y) { 46 return true; 47 } 48 49 //直接将目标位置赋值给图片当前位置。也可以采用以下代码产生动画 50 51 52 //以下代码:按间隔时间、平均移动的距离,缓慢移动图片到目标位置,产生动画效果 53 var dist; 54 if(xpos < final_x){ 55 dist = Math.ceil((final_x - xpos)/10); 56 xpos += dist; 57 } 58 if(xpos > final_x){ 59 dist = Math.ceil((xpos - final_x)/10); 60 xpos -= dist; 61 } 62 if(ypos < final_y){ 63 dist = Math.ceil((final_y - ypos)/10); 64 ypos += dist; 65 } 66 if(ypos > final_y){ 67 dist = Math.ceil((ypos - final_y)/10); 68 ypos -= dist; 69 } 70 elem.style.left = xpos + "px"; 71 elem.style.top = ypos + "px"; 72 var repeat = "movePic('" + pic + "'," + final_x + "," + final_y + "," + interval + ")"; 73 elem.movement = setTimeout(repeat,interval); 74 } 75 //当鼠标移动到小图片上时切换图片 76 function changePic(){ 77 if(!document.getElementById('picfocus')) return false; 78 document.getElementById('picfocus').onmouseover = function(){autokey = true}; 79 document.getElementById('picfocus').onmouseout = function(){autokey = false}; 80 var picbtns = document.getElementById("picbtn").getElementsByTagName("li"); 81 var picnums = picbtns.length; 82 picbtns[0].onmouseover = function(){ 83 movePic('pic',0,0,5); 84 classNormal('picbtn','pictxt'); 85 classCurrent('picbtn','pictxt',0); 86 } 87 if(picnums >= 2){ 88 picbtns[1].onmouseover = function(){ 89 movePic('pic',0,-225,5); 90 classNormal('picbtn','pictxt'); 91 classCurrent('picbtn','pictxt',1); 92 } 93 } 94 if(picnums >= 3){ 95 picbtns[2].onmouseover = function(){ 96 movePic('pic',0,-450,5); 97 classNormal('picbtn','pictxt'); 98 classCurrent('picbtn','pictxt',2); 99 } 100 } 101 if(picnums >= 4){ 102 picbtns[3].onmouseover = function(){ 103 movePic('pic',0,-675,5); 104 classNormal('picbtn','pictxt'); 105 classCurrent('picbtn','pictxt',3); 106 } 107 } 108 } 109 //自动切换图片 110 var autokey = false; 111 setInterval('autoChange()',5000); 112 function autoChange(){ 113 if(autokey) return false; 114 var picbtns = document.getElementById("picbtn").getElementsByTagName("li"); 115 var len = picbtns.length; 116 for(var i = 0; i < len; i++){ 117 if(picbtns[i].className == "current"){ 118 var currentNum = i; 119 } 120 } 121 if(currentNum == 0 && len >= 1){ 122 movePic('pic',0,-225,5); 123 classNormal('picbtn','pictxt'); 124 classCurrent('picbtn','pictxt',1); 125 } 126 if(currentNum == 1 && len >= 2){ 127 movePic('pic',0,-450,5); 128 classNormal('picbtn','pictxt'); 129 classCurrent('picbtn','pictxt',2); 130 } 131 if(currentNum == 2 && len >= 3){ 132 movePic('pic',0,-675,5); 133 classNormal('picbtn','pictxt'); 134 classCurrent('picbtn','pictxt',3); 135 } 136 if(currentNum == 3 && len >= 4){ 137 movePic('pic',0,0,5); 138 classNormal('picbtn','pictxt'); 139 classCurrent('picbtn','pictxt',0); 140 } 141 } 142 addEventLoad(changePic); 143 144 </script>
3:相关html代码为:
1 <body> 2 3 <div id="picfocus"> 4 <div id="piclist"> 5 <div id="pic" style="left:0px; top:0px;"> 6 <ul> 7 <li><a href="#" target="_blank"><img border="0" alt="" src="https://p1.ssl.qhimg.com/t0173f3dea88ac15b71.jpg" /></a></li> 8 <li><a href="#" target="_blank"><img border="0" alt="" src="https://p1.ssl.qhimg.com/t01bef86c511e1f1cec.jpg" /></a></li> 9 <li><a href="#" target="_blank"><img border="0" alt="" src="https://p1.ssl.qhimg.com/t016e4e5efc68cc85d3.jpg" /></a></li> 10 <li><a href="#" target="_blank"><img border="0" alt="" src="https://p2.ssl.qhimg.com/t01a10ab682ccc5b641.jpg" /></a></li> 11 </ul> 12 </div> 13 <div id="pictxtbg"></div> 14 <div id="pictxt"> 15 <ul> 16 <li class="current">刘金山被拒门外踹保安?助理:是劝架</li> 17 <li class="normal">《复仇者联盟》《与妻书》《形影不离》映 </li> 18 <li class="normal">李雪健问鼎百合奖为晚辈蒋梦婕提裙子</li> 19 <li class="normal">刘亦菲携干爹深夜会友人贴面吻告别</li> 20 </ul> 21 </div> 22 </div> 23 <div id="picbtn"> 24 <ul> 25 <li class="current"><img src="https://p2.ssl.qhimg.com/t01a10ab682ccc5b641.jpg" /></li> 26 <li class="normal"><img src="https://p1.ssl.qhimg.com/t016e4e5efc68cc85d3.jpg" /></li> 27 <li class="normal"><img src="https://p1.ssl.qhimg.com/t01bef86c511e1f1cec.jpg" /></li> 28 <li class="normal"><img src="https://p1.ssl.qhimg.com/t0173f3dea88ac15b71.jpg" /></li> 29 </ul> 30 </div> 31 </div> 32 </body>