JavaScript實(shí)現(xiàn)簡(jiǎn)易輪播圖最全代碼解析(ES6面向?qū)ο?
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)簡(jiǎn)易輪播圖的具體代碼,供大家參考,具體內(nèi)容如下
完整代碼:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>ES6輪播圖</title> <script></script> <style> * { margin: 0; padding: 0; } .box { width: 500px; height: 300px; border: 1px solid silver; overflow: hidden; margin: auto; margin-top: 50px; position: relative; top: 0; left: 0; } .outer { list-style: none; position: absolute; top: 0; left: 0; transition: .3s all linear; } .img { width: 500px; height: 300px; float: left; } .btns span { position: absolute; width: 25px; height: 40px; top: 50%; margin-top: -20px; line-height: 40px; text-align: center; font-weight: bold; font-family: Simsun; font-size: 30px; border: 1px solid silver; opacity: 0.5; cursor: pointer; color: #fff; background: black; } .btns .left { left: 5px; } .btns .right { left: 100%; margin-left: -32px; } .right > :first-child, .left > :first-child { width: 35px; height: 35px; } .oOl { width: 163px; position: absolute; right: 0; left: 0; margin: auto; bottom: 10px; list-style: none; } .oLi { width: 25px; height: 25px; background: white; border-radius: 50%; float: left; } .color { background: black; } </style> </head> <body> <div class="box"> <ul class="outer"> <li class="img" style="background-image:url(img/0.jpeg)"></li> <li class="img" style="background-image:url(img/1.jpeg)"></li> <li class="img" style="background-image:url(img/2.jpeg)"></li> <li class="img" style="background-image:url(img/3.jpeg)"></li> <li class="img" style="background-image:url(img/4.jpeg)"></li> </ul> <div class="btns"> <span class="left"><</span> <span class="right">></span> </div> </div> </body> <script> class Chart{ constructor(name, json) { //獲取盒子名 this.box = document.querySelector(name); this.json = json; //獲取輪播圖的屬性 this.outer = document.querySelector(name + ' .outer'); //注意加空格 this.left = document.querySelector(name + ' .left'); this.right = document.querySelector(name + ' .right'); //初始化 this.timer = null; //自動(dòng)播放 this.iNow = 0; this.init(); } init() { const that = this; //保存一個(gè)this console.log(this.json.a); if (this.json.a){ console.log(1); } //克隆第一張放到最后 let uLi = that.outer.children[0].cloneNode(true); that.outer.appendChild(uLi); that.outer.style.width = that.outer.children.length * that.outer.children[0].offsetWidth + 'px'; //點(diǎn)擊左右滑動(dòng) if (that.json.slide) { that.left.style.display = 'block'; that.right.style.display = 'block'; this.left.onclick = () => that.rightGo(); this.right.onclick = () => that.leftGo(); } //自動(dòng)播放 if (that.json.move) { that.moveGo(); //鼠標(biāo)移入移出 if (that.json.loop) { that.box.onmousemove = () => clearInterval(that.timer); that.box.onmouseout = () => that.moveGo(); } } //展示小圓點(diǎn) if (that.json.nav) { let oOL = document.createElement('ol'); oOL.className = 'oOl'; oOL.style.left = that.json.distanceLeft + 'px'; that.box.appendChild(oOL); for (let i = 0; i < that.outer.children.length - 1; i++) { let oLi = document.createElement('li'); oLi.className = 'oLi'; oLi.style.marginLeft = that.json.distance + 'px'; oOL.appendChild(oLi); } oOL.style.width = ((that.outer.children.length - 1) * document.querySelector('.oLi').offsetWidth) + (that.json.distance * that.outer.children.length) + 'px'; that.alike(); } }; rightGo() { this.iNow++; if (this.iNow >= this.outer.children.length) { this.iNow = 1; this.outer.style.transition = '0s all linear'; this.outer.style.left = 0; } this.outer.style.left = -this.iNow * this.outer.children[0].offsetWidth + 'px'; this.outer.style.transition = '0.3s all linear'; this.alike(); }; leftGo() { this.iNow--; if (this.iNow <= -1) { this.iNow = this.outer.children.length - 1; this.outer.style.transition = '0s all linear'; this.outer.style.left = -(this.outer.children.length - 1) * this.outer.children[0].offsetWidth + 'px'; this.iNow = this.outer.children.length - 2; } this.outer.style.left = -this.iNow * this.outer.children[0].offsetWidth + 'px'; this.outer.style.transition = '0.3s all linear'; this.alike(); }; moveGo() { const that = this; this.timer = setInterval(() => that.rightGo(), that.json.speed || 1500) }; //圓點(diǎn)對(duì)應(yīng)每張圖片 alike() { let li = document.querySelectorAll('.oLi'); for (let i = 0; i < li.length; i++) { li[i].classList.remove('color'); if (i == this.iNow) { li[i].classList.add('color'); } else { li[i].classList.remove('color'); } //特殊:最后一張的時(shí)候是第一個(gè) if (this.iNow == li.length) { li[0].classList.add('color'); } //小圓點(diǎn)點(diǎn)擊事件 if (this.json.event) { li[i].onmouseover = () => { for (let i = 0; i < li.length; i++) { li[i].classList.remove('color'); } li[i].classList.add('color'); this.outer.style.left = -i * this.outer.children[0].offsetWidth + 'px'; } } } } } new Chart('.box', { move: true, //自動(dòng)輪播 speed: 1500, //輪播速度 loop: true, //鼠標(biāo)移入移出效果 slide: true, //點(diǎn)擊左右滑動(dòng)效果 nav: true, //展示小圓點(diǎn) distance: 20, //小圓點(diǎn)間距 event: true //小圓點(diǎn)事件 }) </script> </html>
圖片:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript調(diào)用ajax獲取文本文件內(nèi)容實(shí)現(xiàn)代碼
這篇文章主要介紹了JavaScript調(diào)用ajax獲取文本文件內(nèi)容的方法,需要的朋友可以參考下2014-03-03Javascript封裝id、class與元素選擇器方法示例
這篇文章主要給大家介紹了Javascript封裝id、class與元素選擇器的方法,文中給出了詳細(xì)的示例代碼,對(duì)大家的理解和學(xué)習(xí)具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-03-03JS實(shí)現(xiàn)的簡(jiǎn)單標(biāo)簽點(diǎn)擊切換功能示例
這篇文章主要介紹了JS實(shí)現(xiàn)的簡(jiǎn)單標(biāo)簽點(diǎn)擊切換功能,涉及javascript事件響應(yīng)及頁(yè)面元素遍歷、屬性動(dòng)態(tài)變換等相關(guān)操作技巧,需要的朋友可以參考下2017-09-09原生javascript實(shí)現(xiàn)無間縫滾動(dòng)示例
原生javascript無間縫滾動(dòng)目前支持的是豎向與橫向滾動(dòng),下面有個(gè)不錯(cuò)的示例,大家可以參考下2014-01-01利用XMLHTTP傳遞參數(shù)在另一頁(yè)面執(zhí)行并刷新本頁(yè)
利用XMLHTTP傳遞參數(shù)在另一頁(yè)面執(zhí)行并刷新本頁(yè)...2006-10-10JS實(shí)現(xiàn)圖片平面旋轉(zhuǎn)的方法
這篇文章主要介紹了JS實(shí)現(xiàn)圖片平面旋轉(zhuǎn)的方法,涉及JavaScript操作頁(yè)面元素樣式動(dòng)態(tài)變換的相關(guān)技巧,需要的朋友可以參考下2016-03-03微信小程序?qū)崿F(xiàn)登錄注冊(cè)tab切換效果
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)登錄注冊(cè)切換效果,簡(jiǎn)易版tab切換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11