您好,登錄后才能下訂單哦!
這篇文章給大家介紹使用JavaScript怎么實現(xiàn)一個鼠標(biāo)切換圖片,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>圖片切換</title> <style> .picture { position: relative; width: 500px; height: 333px; margin: 0 auto; border: 2px solid rgb(231, 127, 217); overflow: hidden; } .radius { width: 100%; height: 10px; position: absolute; bottom: 30px; text-align: center; } .pg { //圖片上方頁碼 position: absolute; margin: 0; width: 100%; height: 20px; background-color: rgba(0, 0, 0, .4); text-align: center; font-size: 16px; font-weight: 600; color: #fff; } .title { position: absolute; width: 100%; bottom: 0px; text-align: center; font-size: 16px; font-weight: 600; color: rgb(21, 223, 72); } span { display: inline-block; border: 10px solid #fdfdfd; border-radius: 50%; } .active { border: 10px solid #656466; } /* 左右箭頭 */ .arrowhead-left, .arrowhead-right { position: absolute; width: 41px; height: 69px; font-size: 30px; line-height: 70px; text-align: center; color: #D6D8D4; background-color: rgba(0,0,0,.3); } .arrowhead-left { left: 0; top: 40%; } .arrowhead-right { right: 0; top: 40%; } </style> </head> <body> <div class="picture"> <!-- 圖片頁碼 --> <p class="pg">封面</p> <img src="./image/d8.jpeg" alt=""> <!-- 小圓點點 --> <p class="radius"></p> <!-- 圖片的下面標(biāo)題 --> <p class="title">標(biāo)題</p> <!-- 左右箭頭 --> <div class="arrowhead-left" id="al"> < </div> <div class="arrowhead-right" id="ar"> > </div> </div> <script> var address = ["./image/d1.jpeg", "./image/d2.jpeg", "./image/d3.jpeg", "./image/d4.jpeg", "./image/d5.jpeg", "./image/d7.jpeg"]; // var imgs = document.getElementsByTagName("img"); var imgs = document.querySelector("img"); var len = address.length; var str = ""; var pp = document.getElementsByTagName("p");//獲取的是一個集合 // var pp = document.querySelector("p"); //獲取的是一個元素 var al = document.getElementById("al"); var ar = document.getElementById("ar"); //添加span標(biāo)簽 for (i = 0; i < len; i++) { str += ' <span></span>' } console.log(str); console.log(pp); pp[1].innerHTML = str; var spans = pp[1].getElementsByTagName('span'); spans[0].className = 'active'; for (i = 0; i < len; i++) { spans[i].index = i; spans[i].onmouseover = function () { //所有圓點的類為空 for (i = 0; i < len; i++) { spans[i].className = ""; } this.className = 'active'; //給點擊的span(圓點)添加類名 imgs.src = address[this.index]; pp[0].innerHTML = [this.index + 1] + "/6"; pp[2].innerHTML = "風(fēng)光" + [this.index + 1]; } } var n = 0 ; ar.onclick = function () { for (i = 0; i < len; i++) { spans[i].className = ""; } spans[n].className = "active"; imgs.src = address[n]; pp[0].innerHTML = (n+1) + "/6"; pp[2].innerHTML = "風(fēng)光" +(n+1); if (n<5) { n++; } else { n=0; } } al.onclick = function () { for (i = 0; i < len; i++) { spans[i].className = ""; } spans[n].className = "active"; imgs.src = address[n]; pp[0].innerHTML = (n+1) + "/6"; pp[2].innerHTML = "風(fēng)光" +(n+1); if (n>0) { n--; } else {} n=(len-1); } } </script> </body> </html>
關(guān)于使用JavaScript怎么實現(xiàn)一個鼠標(biāo)切換圖片就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。