JS輪播圖的實(shí)現(xiàn)方法2
本文實(shí)例為大家分享了JS輪播圖的實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
需求:
自動(dòng)輪播,鼠標(biāo)移入輪播停止、移出繼續(xù),小圓點(diǎn)點(diǎn)擊切圖,左右箭頭切圖
效果圖:
![輪播圖]
思路:
將所有需要輪播的圖片橫向排列,可視區(qū)大小設(shè)置為只能顯示一張圖片,給容器設(shè)置移出隱藏后,可視區(qū)之外的部分被隱藏,這樣我們就只能看見(jiàn)一張圖片。
輪播實(shí)現(xiàn)是改變整個(gè)圖片畫(huà)幅的left值或者margin-left 值,當(dāng)點(diǎn)擊時(shí)移動(dòng)整個(gè)畫(huà)幅單個(gè)圖片的寬度即可實(shí)現(xiàn)滾動(dòng)到下一張。
后面功能實(shí)現(xiàn)順序依舊是寫(xiě)法一里的思路。
HTML部分:
<div id="banner"> <div class="w"> <!-- 左右箭頭--> <span class="iconfont icon-zuojiantou"></span> <span class="iconfont icon-youjiantou"></span> <!-- 輪播圖--> <ul > <li><img src="img/1.jpg" alt=""></li> <li><img src="img/2.jpg" alt=""></li> <li><img src="img/3.jpg" alt=""></li> </ul> <!-- /小圓點(diǎn)--> <ol id="circleContainer"> </ol> </div> </div>
CSS部分:
<style> *{ margin: 0; padding: 0; list-style: none; } .w { width: 1000px; height: 600px; margin: 100px auto 0; position: relative; overflow: hidden; } ul { width: 1000%; transition: all .5s ease-in-out; } ul li { float: left; width: 1000px; } ul li img { width: 100%; height: 600px; } .iconfont { color: white; position: absolute; font-size: 30px; top: calc(50% - 15px); background-color: rgba(216, 216, 216, 0.23); border-top-right-radius: 50%; border-bottom-right-radius: 50%; cursor: pointer; } .iconfont:hover { color: palegreen; } .icon-zuojiantou { left: 0; } .icon-youjiantou { right: 0; } #circleContainer { position: absolute; bottom: 10px; left: calc(50% - 30px); } #circleContainer li { display: inline-block; width: 20px; height: 20px; border-radius: 50%; background-color: white; margin-right: 5px; } #circleContainer .change { background-color: palegreen; } </style>
JS部分:
<script> let timer = setInterval(function () { arrow_r.onclick(); },2000); let w = document.querySelector(".w"); w.addEventListener("mouseenter",function () { clearInterval(timer); }); w.addEventListener("mouseleave",function () { clearInterval(timer); timer = setInterval(function () { arrow_r.onclick(); },2000); }); //獲取元素 let ul = document.querySelector("ul"); let ol = document.querySelector("#circleContainer"); let arrow_r = document.querySelector(".icon-youjiantou"); let arrow_l = document.querySelector(".icon-zuojiantou"); let ImgWidth = ul.children[0].offsetWidth; let location_i = 0 ; // 創(chuàng)建小圓點(diǎn) 動(dòng)態(tài)生成小圓點(diǎn),圖片增加時(shí)小圓點(diǎn)也隨之增加 for (let i = 0; i<ul.children.length;i++){ let li = document.createElement("li"); li.setAttribute("index",i); li.addEventListener("click",function () { let index = this.getAttribute("index"); move(ul,index); location_i = index ; }); ol.appendChild(li); } let liclone = ul.children[0].cloneNode(true); ul.appendChild(liclone); // 輪播函數(shù) function move(targetObj,n) { if (n === targetObj.children.length ) { targetObj.style.marginLeft = "0px"; n = 0; } targetObj.style.marginLeft = -n * ImgWidth +"px"; for (let i =0 ;i<ol.children.length;i++){ ol.children[i].className = ""; } n === 3 ? ol.children[0].className = "change": ol.children[n].className = "change"; } ol.children[0].className = "change"; // 右箭頭點(diǎn)擊 arrow_r.onclick = function () { if (location_i === 3) { location_i = 0 ; ul.style.marginLeft = "0px" ; } location_i++; move(ul,location_i); }; // 左箭頭點(diǎn)擊 arrow_l.addEventListener("click",function () { if (location_i == 0) { location_i = ul.children.length -1 ; } location_i--; move(ul,location_i); }) </script>
精彩專題分享:jQuery圖片輪播 JavaScript圖片輪播 Bootstrap圖片輪播
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript獲取select值的方法完整實(shí)例
這篇文章主要介紹了javascript獲取select值的方法,結(jié)合完整實(shí)例形式分析了javascript動(dòng)態(tài)遍歷與操作頁(yè)面元素相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-06-06JS?簡(jiǎn)單實(shí)現(xiàn)拖拽評(píng)星的示例代碼
本文主要介紹了JS?簡(jiǎn)單實(shí)現(xiàn)拖拽評(píng)星,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05js prototype深入理解及應(yīng)用實(shí)例分析
這篇文章主要介紹了js prototype深入理解及應(yīng)用,結(jié)合實(shí)例形式分析了JavaScript prototype屬性功能、原理、使用方法及操作注意事項(xiàng),需要的朋友可以參考下2019-11-11JavaScript call apply使用 JavaScript對(duì)象的方法綁定到DOM事件后this指向問(wèn)題
JavaScript對(duì)象與DOM對(duì)象進(jìn)行綁定會(huì)遇到一個(gè)問(wèn)題:如果被綁定的對(duì)象的方法中包含this關(guān)鍵字,當(dāng)事件被觸發(fā)時(shí)this指向的卻是DOM對(duì)象,而不是之前的JS對(duì)象。2011-09-09Javascript 原型和繼承(Prototypes and Inheritance)
前面我們看到了如何使用 constructor 來(lái)初始化對(duì)象。如果這樣做,那么每一個(gè)創(chuàng)建的新對(duì)象都會(huì)對(duì)那些相同的屬性,方法建立一個(gè)獨(dú)立的副本。而實(shí)際上有更加有效的方法來(lái)指定方法,常量,以及其他一些可被所有該類的對(duì)象共享的屬性。2009-04-04Express實(shí)現(xiàn)前端后端通信上傳圖片之存儲(chǔ)數(shù)據(jù)庫(kù)(mysql)傻瓜式教程(一)
這篇文章主要介紹了Express實(shí)現(xiàn)前端后端通信上傳圖片存儲(chǔ)數(shù)據(jù)庫(kù)(mysql)傻瓜式教程(一),需要的朋友可以參考下2015-12-12