js圖片輪播與索引變色
阿新 • • 發佈:2017-07-06
shu gin 標題 solid ansi name 素材 ++ 切換
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文檔</title> <style type="text/css"> #tupian{ width:290px; height:160px; border:#0F9 solid 1px} .tp{ width:220px; height:160px; float:left} .li{ margin-top:10px;list-style:none; border:#00F solid 1px; width:20px;} </style> </head> <body> <div id="tupian"> <div class="tp" onmouseover="sbfs()" onmouseout="jixu()"><img src="../../項目素材/圖片/84372.jpg" /></div> <div class="tp" onmouseover="sbfs()" onmouseout="jixu()" style="display:none" ><img src="../../項目素材/圖片/84610.jpg" /></div> <div class="tp" onmouseover="sbfs()" onmouseout="jixu()" style="display:none"><img src="file:///C|/Users/gaofangquan/Desktop/項目素材/圖片/84246.jpg" /></div> <div class="tp" onmouseover="sbfs()" onmouseout="jixu()" style="display:none"><img src="../../項目素材/圖片/84639 (1).jpg" /></div> <div id="xuanxiang" style="float:right; "> <ul> <li class="li" onmouseover="xz(this),xuanzhong(this)" onmouseout="shuzilikai()">1</li> <li class="li" onmouseover="xz(this),xuanzhong(this)" onmouseout="shuzilikai()">2</li> <li class="li" onmouseover="xz(this),xuanzhong(this)" onmouseout="shuzilikai()">3</li> <li class="li" onmouseover="xz(this),xuanzhong(this)" onmouseout="shuzilikai()">4</li> </ul> </div> </div> </body> <script type="text/jscript"> var aaaa=document.getElementsByClassName("tp") var bbb=document.getElementsByClassName("li") var index=0; lunbo(); function lunbo(){ xianshi(); if( index<aaaa.length-1){ index++;} else{index=0;}} var a=window.setInterval("lunbo()",1000) //圖片與數列的背景同時變化 function xianshi(){ for( var i=0;i<aaaa.length;i++){ aaaa[i].style.display="none" bbb[i].style.backgroundColor="white"} aaaa[index].style.display="block" bbb[index].style.backgroundColor="red"} //鼠標放在數列上背景變色function xz(n){ var b=document.getElementsByClassName("li") for( var i=0; i<b.length; i++){ b[i].style.backgroundColor="white"} n.style.backgroundColor="red"} //當鼠標放到圖片上的時候,圖片停止輪播 function sbfs(){ window.clearInterval(a)} //當鼠標離開圖片的時候,輪播繼續 function jixu(){ a=window.setInterval("lunbo()",1000);} //鼠標放上,圖片與相對應的li的值得索引顯示出來 function xuanzhong(m){ var ccc=document.getElementsByClassName("tp") index=m.innerHTML-1;// 因為m.innerhtml 獲取的時數列li的值,比ccc的索引大1,所以應該減去1; //把獲取的值賦給index是因為如果重新定義一個變量的話,當鼠標離開li的時候,圖片輪播的索引很亂, for( var i=0;i<ccc.length;i++){ ccc[i].style.display="none"} ccc[index].style.display="block" window.clearInterval(a)} //鼠標離開讓圖片自動切換 function shuzilikai(){ a=window.setInterval("lunbo()",1000)} </script> </html>
1、js圖片輪播首先要先獲取所有圖片的對象,用數組接受,然後定義一個變量作為數組的索引。
2、讓圖片顯示(先讓所有的圖片隱藏,再讓當前圖片顯示),索引自增1。註意:在寫定時器的時候可以寫延時的也可以寫間歇的。在這裏我寫間歇的。
3、鼠標放在圖片上停止輪播,這裏我們可以清除定時器window.clearInterval(a);鼠標離開圖片輪播繼續再重新啟動定時器a=window.setInterval("lunbo()",1000),這裏註意要給定時器定義一個值,因為清除定時器要用。
4、鼠標放上,圖片與相對應的li的值得索引顯示出來,讓獲取的數列的值賦給數組的索引(index=m.innerHTML-1),因為m.innerhtml 獲取的時數列li的值,比數組的索引大1,所以應該減去1;
js圖片輪播與索引變色