本文實(shí)例為大家分享了javascript實(shí)現(xiàn)圖片輪播特效,供大家參考,具體內(nèi)容如下
一、實(shí)現(xiàn)效果

如上圖:
1、圖片自動(dòng)依次輪換,每輪換到一張圖片,下面對(duì)應(yīng)的小圖標(biāo)出現(xiàn)紅色邊框,并顯示對(duì)應(yīng)的圖片名稱(chēng)
2、鼠標(biāo)放到大圖片上面的時(shí),圖片停止輪換,一直顯示當(dāng)前圖片;鼠標(biāo)移開(kāi)后圖片繼續(xù)輪換
3、鼠標(biāo)移到小圖標(biāo)上時(shí),大圖片區(qū)域會(huì)顯示對(duì)應(yīng)的大圖;鼠標(biāo)移開(kāi)則從當(dāng)前圖片開(kāi)始繼續(xù)輪換
二、代碼
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title>帶小圖標(biāo)的JS圖片輪換</title> <style type="text/css"> *{ margin: 0px; padding: 0px; } #content{ width: 700px; height: 538px; margin: 0px auto; /*使所有內(nèi)容居中*/ border: solid #F0F0F0; } /*定義下面小圖標(biāo)處樣式*/ #nav1 table{ width: 100%; height: 35px; margin-top: -4px; } #nav1 td{ width: 35px; height: 35px; text-align: center; /*文字居中*/ color: #ffffff; } #text{ } #_text{ width: 100%; height: 100%; background-color: #F0F0F0; border: none; text-align: center; font-size: 18px; color: #000000; font-weight: bold; } </style></head><body onload="changeImg()"> <div id="content"> <div id="images"> <img id="_photoes" src="../images/textPhoto/1.jpg" height="500px" width="700px" onmouseover="over1()" onmouseout="out1()"> </div> <div id="nav1"> <table border="0"> <tr> <td id="text" bgcolor="#F0F0F0" colspan="15"><input type="text" id="_text" readonly></td> <td id="img1" class="sImg" bgcolor="#db7093" onmouseover="over2(1)" onmouseout="out2(1)">1</td> <td id="img2" class="sImg" bgcolor="#da70d6" onmouseover="over2(2)" onmouseout="out2(2)">2</td> <td id="img3" class="sImg" bgcolor="#9acd32" onmouseover="over2(3)" onmouseout="out2(3)">3</td> <td id="img4" class="sImg" bgcolor="#adff2f" onmouseover="over2(4)" onmouseout="out2(4)">4</td> <td id="img5" class="sImg" bgcolor="#00bfff" onmouseover="over2(5)" onmouseout="out2(5)">5</td> </tr> </table> </div> </div> <script type="text/javascript"> //將輪換的大圖片放入數(shù)組中 var arr = new Array(); arr[0] = "../images/textPhoto/1.jpg"; arr[1] = "../images/textPhoto/2.jpg"; arr[2] = "../images/textPhoto/3.jpg"; arr[3] = "../images/textPhoto/4.jpg"; arr[4] = "../images/textPhoto/5.jpg"; //將input區(qū)域變換的文字放在數(shù)組里 var text = new Array(); text[0] = "焦點(diǎn)圖1"; text[1] = "焦點(diǎn)圖2"; text[2] = "焦點(diǎn)圖3"; text[3] = "焦點(diǎn)圖4"; text[4] = "焦點(diǎn)圖5"; var smallImg = document.getElementsByClassName("sImg"); //將頁(yè)面上所有小圖片存放在一個(gè)數(shù)組 var num = 0; function changeImg() { document.getElementById("_photoes").src = arr[num]; document.getElementById("_text").value = text[num]; //當(dāng)前小圖標(biāo)增加邊框樣式 for(var i=0;i<arr.length;i++) { if(i==num) smallImg[num].style.border = "red solid"; //大圖標(biāo)對(duì)應(yīng)的小圖標(biāo)增加邊框樣式 else smallImg[i].style.border = "none"; } if (num == arr.length - 1) num = 0; //如果顯示的是最后一張圖片,則圖片序號(hào)變?yōu)榈谝粡埖男蛱?hào) else num += 1; //圖片序號(hào)加一 } var setID = setInterval("changeImg()",1000); //這樣寫(xiě)任然會(huì)不斷調(diào)用changeImg()函數(shù) /*當(dāng)鼠標(biāo)滑到大圖標(biāo)上時(shí)*/ function over1() { clearInterval(setID); //圖片停止輪換// smallImg[n-1].style.border = "red solid"; } /*當(dāng)鼠標(biāo)離開(kāi)大圖標(biāo)時(shí)*/ function out1() { setID = setInterval("changeImg()",1000); //圖片繼續(xù)輪換// smallImg[n-1].style.border = "none"; //大圖標(biāo)對(duì)應(yīng)的小圖標(biāo)邊框樣式取消 } /*當(dāng)鼠標(biāo)滑到小圖標(biāo)上時(shí)*/ function over2(n) { document.getElementById("_photoes").src = arr[n-1]; //只要鼠標(biāo)滑到小圖標(biāo)上,大圖區(qū)域就顯示對(duì)應(yīng)的圖片 document.getElementById("_text").value = text[n-1]; clearInterval(setID); //圖片停止輪換 //當(dāng)前小圖標(biāo)增加邊框樣式 for(var i=0;i<arr.length;i++) { if(i==n-1) smallImg[n-1].style.border = "red solid"; else smallImg[i].style.border = "none"; } } /*當(dāng)鼠標(biāo)離開(kāi)小圖標(biāo)時(shí)*/ function out2(n) { if(n!=arr.length) num = n; //從當(dāng)前圖片開(kāi)始輪換 else num = 0; setID = setInterval("changeImg()",1000); //圖片繼續(xù)輪換// smallImg[n-1].style.border = "none"; //小圖標(biāo)邊框樣式取消 } </script></body></html>三、多張圖片輪換調(diào)試筆記
js源代碼:
//實(shí)現(xiàn)幾張圖片的輪換var num = 0; //顯示的圖片序號(hào),剛開(kāi)始時(shí)是第一張圖片function changeImg1() { var arr = new Array(); arr[0]="../images/hao123/7.jpg"; arr[1]="../images/hao123/8.jpg"; arr[2]="../images/hao123/9.jpg"; var photo = document.getElementById("topPhoto"); if (num == arr.length - 1) num = 0; //如果顯示的是最后一張圖片,則圖片序號(hào)變?yōu)榈谝粡埖男蛱?hào) else num += 1; //圖片序號(hào)加一 photo.src = arr[num];}setInterval("changeImg1()",5000); //每隔5000毫秒調(diào)用一次changImg1()函數(shù) HTML代碼:
<img src="../images/hao123/7.jpg" id="topPhoto">
在使用的時(shí)候最好定義一下圖片的樣式,把圖片的長(zhǎng)寬都統(tǒng)一,這樣圖片動(dòng)態(tài)顯示的效果會(huì)更好一些。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)javascript程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注