標籤:注意 oct set 定義 nbsp name javascrip 素材 i++
<!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圖片輪播與索引變色