JS輪播圖實現(xiàn)簡單代碼
本文實例為大家分享了js輪播圖實現(xiàn)代碼,供大家參考,具體內(nèi)容如下
思路:
1、首先要有個盛放圖片的容器,設置為單幅圖片的寬高,且overflow:hidden,這樣保證每次可以只顯示一個圖片
2、Container內(nèi)有個放圖片的list進行position的定位 ,其中的圖片采用float的方式,同時當圖片進行輪播時,改變list的Left值使得其顯示的圖片發(fā)生變化。
3、圖片的輪播使用定時器,通過定時器改變list的Left值是的圖片循環(huán)展示
4、當鼠標滑動到圖片上時,清除定時器,圖片停止輪播,當鼠標移出時繼續(xù)進行輪播
5、圖片上有一組小圓點用于與當前顯示的圖片相對應,同時可以通過點擊的方式查看對應的圖片
6、圖片可以通過點擊進行左右滑動顯示
代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>輪播圖</title> <style type="text/css"> .container{ margin:0 auto; width:600px; height:400px; position: relative; overflow: hidden; border:4px solid gray; box-shadow: 3px 3px 5px gray; border-radius:10px; } .list{ width:4200px; height:400px; position: absolute; top:0px; } img{ float:left; width:600px; height:400px; } .dots{ position: absolute; left:40%; bottom:30px; list-style: none; } .dots li{ float:left; width:8px; height:8px; border-radius: 50%; background: gray; margin-left:5px } .dots .active{ background: white; } .pre,.next{ position: absolute; top:40%; font-size:40px; color:white; text-align:center; background: rgba(128,128,128,0.5); /* display:none;*/ } .pre{ left:30px; } .next{ right:30px; } </style> </head> <body> <div class="container"> <div class="list" style=" left:-600px;"> <img src="img/5.jpg"> <img src="img/1.jpg"> <img src="img/2.jpg"> <img src="img/3.jpg"> <img src="img/4.jpg"> <img src="img/5.jpg"> <img src="img/1.jpg"> </div> <ul class="dots"> <li index=1 class="active dot"></li> <li index=2 class="dot"></li> <li index=3 class="dot"></li> <li index=4 class="dot"></li> <li index=5 class="dot"></li> </ul> <div class="pre"><</div> <div class="next">></div> </div> <script type="text/javascript"> var index=1,timer; function init(){ eventBind(); autoPlay(); } init(); function autoPlay(){ timer =setInterval(function () { animation(-600); dotIndex(true); },1000) } function stopAutoPlay() { clearInterval(timer); } function dotIndex(add){ if(add){ index++; } else{ index--; } if(index>5){ index = 1; } if(index<1){ index = 5; } dotActive(); } function dotActive() { var dots = document.getElementsByClassName("dot"); var len = dots.length; for(var i=0 ;i<len ;i++){ dots[i].className = "dot"; } for(var i=0;i<len;i++){ /*此處可以不用parseInt,當不用全等時*/ if(index === parseInt(dots[i].getAttribute("index"))){ dots[i].className = "dot active"; } } } function eventBind(){ /*點的點擊事件*/ var dots = document.getElementsByClassName("dot"); var len = dots.length; for(var i=0;i<len;i++){ (function(j){ dots[j].onclick = function(e){ var ind = parseInt(dots[j].getAttribute("index")); animation((index - ind)*(-600));/*顯示點擊的圖片*/ index = ind; dotActive(); } })(i) } /*容器的hover事件*/ var con = document.getElementsByClassName("container")[0]; /*鼠標移動到容器上時,停止制動滑動,離開時繼續(xù)滾動*/ con.onmouseover = function (e) { stopAutoPlay(); } con.onmouseout =function(e){ autoPlay(); } /*箭頭事件的綁定*/ var pre = document.getElementsByClassName("pre")[0]; var next = document.getElementsByClassName("next")[0]; pre.onclick = function (e) { dotIndex(false); animation(600); } next.onclick = function (e) { dotIndex(true); animation(-600); } } function animation(offset){ var lists = document.getElementsByClassName("list")[0]; var left = parseInt(lists.style.left.slice(0,lists.style.left.indexOf("p"))) + offset; if(left<-3000){ lists.style.left = "-600px"; } else if(left>-600){ lists.style.left = "-3000px"; } else{ lists.style.left = left+"px"; } } </script> </body> </html>
精彩專題分享:jQuery圖片輪播 JavaScript圖片輪播 Bootstrap圖片輪播
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
JavaScript實現(xiàn)數(shù)字數(shù)組正序排列的方法
這篇文章主要介紹了JavaScript實現(xiàn)數(shù)字數(shù)組正序排列的方法,涉及javascript中sort方法的使用技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04教你JS更簡單的獲取表單中數(shù)據(jù)(formdata)
這篇文章主要介紹了JS更簡單的獲取表單中數(shù)據(jù)(formdata),本文給大家分享的js獲取表單數(shù)據(jù)更簡潔,通過兩種方法結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-05-05分離與繼承的思想實現(xiàn)圖片上傳后的預覽功能:ImageUploadView
本文要介紹的是網(wǎng)頁中常見的圖片上傳后直接在頁面生成小圖預覽的實現(xiàn)思路,考慮到該功能有一定的適用性,于是把相關的邏輯封裝成了一個ImageUploadView組件,實際使用效果可查看下一段的git效果圖2016-04-04通過函數(shù)作用域和塊級作用域看javascript的作用域鏈
這篇文章給大家分享了通過函數(shù)作用域和塊級作用域看javascript的作用域鏈的相關知識點內(nèi)容,有興趣的朋友參考學習下。2018-08-08JavaScript利用事件循環(huán)實現(xiàn)數(shù)據(jù)預加載
當我們請求好第一頁數(shù)據(jù)后,將請求下一頁放到異步任務隊列里(宏任務或者微任務),等當前同步任務執(zhí)行完成后,立馬請求下一頁數(shù)據(jù),本文給大家介紹了JavaScript如何利用事件循環(huán)實現(xiàn)數(shù)據(jù)預加載,需要的朋友可以參考下2024-05-05