首页 / ES6 / es6的class类封装轮播图
es6的class类封装轮播图
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了es6的class类封装轮播图,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4815字,纯文字阅读大概需要7分钟。
内容图文
![es6的class类封装轮播图](/upload/InfoBanner/zyjiaocheng/1209/62e5da8ca59344ba840938be73b8fab3.jpg)
话不多说,直接上代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script></script> <style> * { margin: 0; padding: 0; } .box { width: 400px; height: 350px; border: 1px solid red; 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: 400px; height: 350px; float: left; } .left { position: absolute; top: 200px; right: 0; display: none; } .right { position: absolute; top: 200px; left: 0; display: none; } .right > :first-child, .left > :first-child { width: 35px; height: 35px; } .oOl { list-style: none; position: absolute; bottom: 30px; } .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: rebeccapurple">1</li> <li class="img" style="background: orangered">2</li> <li class="img" style="background: yellow">3</li> <li class="img" style="background: green">4</li> <li class="img" style="background: greenyellow">5</li> </ul> <a href="javascript: void (0);" class="left"><img src="./images/1.png" ></a> <a href="javascript: void (0);" class="right"><img src="./images/2.png" ></a> </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; //自动播放this.iNow = 0; this.init(); }; init() { const that = this; //保存一个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‘; //点击左右滑动if (that.json.slide) { that.left.style.display = ‘block‘; that.right.style.display = ‘block‘; this.left.onclick = () => { that.leftGo() }; this.right.onclick = () => { that.rightGo() }; } //自动播放if (that.json.move) { that.moveGo(); //鼠标移入移出if (that.json.loop) { that.box.onmousemove = () => { clearInterval(that.timer) }; that.box.onmouseout = () => { that.moveGo() } } } //展示小圆点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(); } }; leftGo() { 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 = ‘.3s all linear‘; this.alike(); }; rightGo() { 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‘; }; moveGo() { const that = this; this.timer = setInterval(() => { that.leftGo(); }, that.json.speed || 1500) }; //圆点对应每张图片 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‘); } //特殊:最后一张的时候是第一个if (this.iNow == li.length) { li[0].classList.add(‘color‘); } //小圆点点击事件if (this.json.event) { li[i].onclick = () => { 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, //自动轮播 speed: 1500, //轮播速度 loop: true, //鼠标移入移出效果 slide: true, //点击左右滑动效果 nav: true, //展示小圆点 distanceLeft: 76, //小圆点距离左边的距离 distance: 20, //小圆点间距 event: true//小圆点事件 }) </script> </html>
原文:https://www.cnblogs.com/zhanping/p/11574035.html
内容总结
以上是互联网集市为您收集整理的es6的class类封装轮播图全部内容,希望文章能够帮你解决es6的class类封装轮播图所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。