Html+CSS+JS輪播圖實現(xiàn)源碼(手動輪播,自動輪播)
更新時間:2023年06月19日 11:21:17 作者:emm_10_01
今天做網(wǎng)站的時候需要用上JS輪播圖代碼,而且還要求是原生的JS代碼,下面這篇文章主要給大家介紹了關(guān)于Html+CSS+JS輪播圖實現(xiàn)的相關(guān)資料,文中介紹的方法包括手動輪播和自動輪播,需要的朋友可以參考下
演示效果
輪播圖代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .banner { width: 500px; height: 300px; position: relative; /* border: 1px solid red; */ margin: 100px auto; } .banner .wrap { width: 100%; } .banner .wrap .item { width: 100%; } .item img { width: 500px; height: 300px; vertical-align: top; position: absolute; } .div1 { position: absolute; left: 0; top: 50%; transform: translatey(-50%); cursor: pointer; width: 41px; height: 69px; font-size: 30px; line-height: 70px; text-align: center; color: #D6D8D4; background-color: rgba(0, 0, 0, 0.3); } .div2 { position: absolute; right: 0; top: 50%; transform: translatey(-50%); cursor: pointer; width: 41px; height: 69px; font-size: 30px; line-height: 70px; text-align: center; color: #D6D8D4; background-color: rgba(0, 0, 0, 0.3); } .pagenation { position: absolute; /* border: 1px solid red; */ left: 50%; transform: translateX(-50%); display: flex; bottom: 40px; } .pagenation>div { width: 10px; height: 10px; border-radius: 50%; background-color: white; margin-right: 10px; cursor: pointer; } .pagenation>div:last-child { margin-right: 0; } </style> </head> <body> <div class="banner"> <div class="warp"> <div class="item"><img src="./img/1.jpg" alt=""></div> <div class="item"><img src="./img/2.jpg" alt=""></div> <div class="item"><img src="./img/3.jpg" alt=""></div> <div class="item"><img src="./img/4.jpg" alt=""></div> </div> <div class="div1"> <</div> <div class="div2 ">> </div> <!-- 小圓點 --> <div class="pagenation"> <div id="pagenation-item0"></div> <div id="pagenation-item1"></div> <div id="pagenation-item2"></div> <div id="pagenation-item3"></div> </div> </div> <script> var index = 0; //定義初始下標(biāo) var banner = document.getElementsByClassName("banner")[0]; var itemList = document.getElementsByClassName("item"); var pagenationList = document.querySelectorAll(".pagenation>div"); var pagenation = document.querySelector(".pagenation"); itemList[0].style.opacity = 1; pagenationList[0].style.background = "blue" //初始第一張圖對應(yīng)的小圓點的顏色為藍(lán)色 var up = document.getElementsByClassName("div1")[0]; var next = document.getElementsByClassName("div2")[0]; //下一張圖片(封裝方便下面自動播放定時器調(diào)用) function nextFn() { index = index >= itemList.length - 1 ? 0 : ++index; //判斷點擊到了最后一張圖片再次點擊返回到第一張圖 for (var i = 0; i < itemList.length; i++) { itemList[i].style.opacity = 0; //圖片透明度為0圖片隱藏 pagenationList[i].style.background = "white " //圖片沒顯現(xiàn)小圓點的顏色為白色 } itemList[index].style.transition = "opacity 1s ease .2s" itemList[index].style.opacity = 1; //圖片透明度為1圖片顯現(xiàn) pagenationList[index].style.background = "blue" //圖片顯現(xiàn)小圓點的顏色為藍(lán)色 } next.onclick = nextFn; //下一張(點擊事件)點擊切換下一張圖片 up.onclick = function () { //上一張(點擊事件)點擊切換上一張圖片 index = index <= 0 ? itemList.length - 1 : --index; //判斷點擊到了第一張圖片再次點擊返回到最后一張圖 for (var i = 0; i < itemList.length; i++) { itemList[i].style.opacity = 0; pagenationList[i].style.background = "white" } itemList[index].style.transition = "opacity 1s ease .2s" //增加過渡效果 itemList[index].style.opacity = 1; pagenationList[index].style.background = "blue" } //設(shè)置定時器,自動向下播放圖片 var t1 = setInterval(nextFn, 2000) banner.onmouseover = function () { clearInterval(t1); } banner.onmouseout = function () { t1 = setInterval(nextFn, 2000) } // 事件委托 pagenation.onclick = function (event) { event = window.event || event console.log(event); if (event.target.className == "pagenation") { console.log("點擊的是父元素"); } else { var id = event.target.id; var photoIndex = null; for (var i = 0; i < pagenationList.length; i++) { pagenationList[i].style.background = "white" if (id.indexOf(i) >= 0) { photoIndex = i; } } event.target.style.background = "blue" index = photoIndex; // 將小圓點對應(yīng)的下標(biāo)與圖片下標(biāo)對應(yīng) for (var i = 0; i < itemList.length; i++) { itemList[i].style.opacity = 0; } itemList[index].style.transition = "opacity 1s ease .2s" itemList[photoIndex].style.opacity = 1; } } </script> </body> </html>
總結(jié)
到此這篇關(guān)于Html+CSS+JS輪播圖實現(xiàn)的文章就介紹到這了,更多相關(guān)Html+CSS+JS輪播圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
jquery插件jquery.confirm彈出確認(rèn)消息
這篇文章介紹了插件jquery.confirm彈出確認(rèn)消息的實現(xiàn)方法,感興趣的小伙伴們可以參考一下2015-12-12JavaScript實現(xiàn)倒計時代碼段Item1(非常實用)
現(xiàn)今團(tuán)購網(wǎng)、電商網(wǎng)、門戶網(wǎng)等,常使用時間記錄重要的時刻,如時間顯示、倒計時差、限時搶購等,本文分析不同倒計時效果的計算思路及方法,掌握日期對象Date,獲取時間的方法,計算時差的方法,實現(xiàn)不同的倒時計效果2015-11-11JavaScript數(shù)據(jù)類型區(qū)別及檢測方法
在JavaScript中,數(shù)據(jù)類型是編程中非常重要的概念,它決定了數(shù)據(jù)的性質(zhì)、如何存儲以及如何操作這些數(shù)據(jù),本文介紹JavaScript數(shù)據(jù)類型區(qū)別及檢測方法,感興趣的朋友一起看看吧2024-04-04javascript中關(guān)于&& 和 || 表達(dá)式的小技巧分享
我將會介紹和解析12個簡單但是強大的JavaScript技巧. 這些技巧所有的JavaScript程序員都可以馬上使用, 你不需要成為JavaScript高手才能理解這些.下面我們開始本系列的第一篇文章,介紹下強大的&& 和 || 表達(dá)式2015-04-04JavaScript代碼判斷輸入的字符串是否含有特殊字符和表情代碼實例
這篇文章主要介紹了JavaScript代碼判斷輸入的字符串是否含有特殊字符和表情,通過js代碼if語句進(jìn)行判斷,并結(jié)合自己開發(fā)的情景,具體操作步驟大家可查看下文的詳細(xì)講解,感興趣的小伙伴們可以參考一下。2017-08-08