Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize simple Carousel Graph with JavaScript

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

这篇文章主要介绍了JavaScript如何实现简易轮播图,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

具体内容如下

完整代码:

ES6轮播图 * { 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; } 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.rightGo(); this.right.onclick = () => that.leftGo(); } //自动播放 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(); } }; 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 that.rightGo(), that.json.speed || 1500) }; //dots correspond to each picture 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].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, //auto carousel speed: 1500, //carousel speed loop: true, //mouse in and out effect slide: true, //click left and right slide effect nav: true, //Show dots distance: 20, //dot spacing event: true //dot event })

Picture:

Thank you for reading this article carefully. I hope that the article "How to Realize Simple Carousel Map in JavaScript" shared by Xiaobian will be helpful to everyone. At the same time, I hope that everyone will support it a lot and pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report