您好,登錄后才能下訂單哦!
這篇文章主要介紹“如何用JavaScript實現輪播圖效果”,在日常操作中,相信很多人在如何用JavaScript實現輪播圖效果問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何用JavaScript實現輪播圖效果”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
實現代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> * { margin: 0; padding: 0; } ul { list-style: none; } #box { margin: 30px auto; width: 590px; height: 340px; position: relative; } #banner-list > li { position: absolute; left: 0; right: 0; opacity: 0; /*過渡動畫*/ transition: opacity 2s ease; } #left, #right { width: 30px; height: 60px; text-align: center; line-height: 60px; font-size: 24px; color: rgba(255,255,255,0.7); background-color: rgba(0,0,0,0.3); position: absolute; top: 50%; margin-top: -30px; z-index: 3; } #right { right: 0; } #tag-list { width: 130px; position: absolute; left: 50%; bottom: 8px; margin-left: -55px; } #tag-list > li { float: left; width: 18px; height: 18px; margin: 4px; text-align: center; line-height: 18px; font-size: 13px; background-color: white; border-radius: 9px; /*過渡動畫*/ transition: background-color 1s ease; } </style> <script> window.onload = function (){ //獲取tag_list和圓圈list var tag_list = document.getElementById("tag-list"); var list = tag_list.children; //1.獲取 ul(banner_list) 和 所有的li let banner_list = document.getElementById("banner-list"); let bannerLi = banner_list.children; //2.默認顯示第一張banner bannerLi[0].className = "current-banner" bannerLi[0].style.opacity = 1 list[0].style.backgroundColor = "red" //3.索引從0開始,默認顯示第一張。 //count表示當前顯示頁面的索引 let count = 0; //4.點擊>向右切換 var right = document.getElementById("right"); right.onclick = function (){ //4.1先將當前頁面隱藏 bannerLi[count].className = "" bannerLi[count].style.opacity = 0 list[count].style.backgroundColor = "white" //4.2.頁碼加1,當到第6張(index=5),切換到第一張(index=0) if (++count == 5){ count = 0 } //4.3 設置當前頁碼為顯示 bannerLi[count].className = "current-banner" bannerLi[count].style.opacity = 1 list[count].style.backgroundColor = "red" } //和right差不多,修改下條件 var left = document.getElementById("left"); left.onclick = function (){ //4.1先將當前頁面隱藏 bannerLi[count].className = "" bannerLi[count].style.opacity = 0 list[count].style.backgroundColor = "white" //4.2.頁碼減1,當到第0張(index=-1),切換到第5張(index=4) if (--count == -1){ count = 4 } //4.3 設置當前頁碼為顯示 bannerLi[count].className = "current-banner" bannerLi[count].style.opacity = 1 list[count].style.backgroundColor = "red" } //給所有圓圈綁定鼠標事件 for (let i = 0; i < list.length; i++) { list[i].onmouseenter= function (){ //設置圓圈樣式 list[count].style.backgroundColor = "white" list[i].style.backgroundColor = "red" //設置banner圖樣式 bannerLi[count].className = "" bannerLi[count].style.opacity = 0 bannerLi[i].className = "current-banner" bannerLi[i].style.opacity = 1 //將count置為i count = i } } } </script> </head> <body> <div id="box"> <ul id="banner-list"> <li class="current-banner"><img src="banner-img/11.jpg" alt=""></li> <li><img src="banner-img/22.jpg" alt=""></li> <li><img src="banner-img/33.jpg" alt=""></li> <li><img src="banner-img/44.jpg" alt=""></li> <li><img src="banner-img/55.jpg" alt=""></li> </ul> <span id="left"><</span> <span id="right">></span> <div> <ul id="tag-list"> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> </div> </div> </body> </html>
到此,關于“如何用JavaScript實現輪播圖效果”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。