html代碼css
<div id="slide" style="height:200px;width:300px;height:100px;border:1px solid #ccc"> <div class="room"> <img src="http://chenjianguang.com/static/callName/images/1.png" alt="" class="slide-item"> <img src="http://chenjianguang.com/static/callName/images/2.png" alt="" class="slide-item"> <img src="http://chenjianguang.com/static/callName/images/3.png" alt="" class="slide-item"> <img src="http://chenjianguang.com/static/callName/images/4.png" alt="" class="slide-item"> <img src="http://chenjianguang.com/static/callName/images/5.png" alt="" class="slide-item"> </div> </div>
必須須要設置:包裹元素的id,以及高度跟寬度html
js代碼:git
//須要先引入插件es6
<script src="./slid-es5.js"></script> <script> slide = new Slide('slide',3000,1,20); </script>
//第一個參數是設置了輪播的包裹元素的id,是必須參數
//第二個參數是輪播間隔,非必須參數,默認爲3000ms
//第三個參數是輪播速度, 非必須參數,默認爲1,數值越大輪播速度越快
//第四個參數是手指滑動靈敏度,非必須參數,默認爲20,數值越大須要滑動觸發的距離就越大github
這是上面代碼的slide Demoapp
廢話很少說,下面實現代碼跟註釋說明ide
(function () { let Slide = function ( slideId = 'slide', timeOut = 3000, speed = 0.1, d = 20 ) { this.d = d;//滑動靈敏度 this.timeOut = timeOut;//輪播間隔 this.slide = document.querySelector('#slide'); //獲取視窗層 this.room = this.slide.querySelector('div'); //獲取內容層 this.imgs = [...this.slide.querySelectorAll('.slide-item')]; //輪播圖元素組 this.slideWidth = parseInt(getComputedStyle(this.slide).width); //視窗層高度 this.slideHeight = parseInt(getComputedStyle(this.slide).height); //視窗層寬度 this.startPoint = null; //手指觸摸的起點 this.navButtons = null; //獲取到全部的導航圓點 this.timer = null; //定時器 this.imgIndex = 1; //當前輪播圖片index this.prev = null; //上一個 this.next = null; //下一個 this.speed = -(this.slideWidth / 10 * speed); //動畫速度 this.resetCss(); //初始化css樣式 this.resetAll(); //初始化設置 this.imgs.forEach((el) => { el.style.height = `${this.slideHeight}px`; el.style.width = `${this.slideWidth}px`; }); //使圖片的寬度跟視窗層同樣 this.autoStart = setInterval(() => { this.nextItem() }, timeOut); //輪播間隔定時器 for (let i = 0; i < this.navButtons.length; i++) { this.navButtons[i].onclick = this.navButtonClick.bind(this); } //綁定導航圓點點擊事件 //綁定觸摸開始事件 this.slide.addEventListener('touchstart', this.touchstart.bind(this)); //綁定觸摸結束事件 this.slide.addEventListener('touchend', this.touchend.bind(this)); } //初始化樣式 Slide.prototype.resetCss = function () { let styleEle = document.createElement('style'); //建立style標籤 let navLeft = this.slideWidth / 2 - this.imgs.length * 9; let navTop = this.slideHeight / 40; styleEle.innerHTML += '.slide{position:relative;overflow:hidden;font-size:0;}'; //溫馨化slide視窗的樣式 styleEle.innerHTML += '.slide .room{position:absolute;}'; //初始化room內容層的樣式 styleEle.innerHTML += '.slide .slide-item{display:inline-block;}'; //設置slide-item的樣式爲內聯塊級元素 styleEle.innerHTML += `.slide .nav{padding:0 4px;list-style:none;position:absolute;font-size:0px;bottom:${navTop}px;left:${navLeft}px;text-align:center;}`; styleEle.innerHTML += '.navButton{display:inline-block;margin:6px 4px;background:#fff;width:8px;height:8px;-moz-border-radius:8px;border-radius:8px;cursor:pointer;box-shadow:0 0 6px #bbb;}'; document.head.appendChild(styleEle); //插入標籤 } Slide.prototype.resetAll = function () { this.slide.setAttribute('class', 'slide'); //給slide元素增長class屬性 this.room.setAttribute('class', 'room'); //給room元素增長class屬性 this.room.style.left = `0px`; //設置room的初始距離 this.room.style.width = `${this.slideWidth * (this.imgs.length)}px`; //設置內容層的寬度 //建立導航圓點定位框 let nav = document.createElement('ul'); nav.id = 'nav'; nav.setAttribute('class', 'nav'); this.slide.appendChild(nav); for (let i = 0; i < this.imgs.length; i++) { let navButtonLi = document.createElement('li'); navButtonLi.setAttribute('class', 'navButton'); navButtonLi.index = i + 1; nav.appendChild(navButtonLi); } this.navButtons = this.slide.querySelectorAll('li'); //獲取全部的li節點 this.navButtons[0].style.background = '#333'; //初始化第一個圓點的顏色 } //切換下一個的函數 Slide.prototype.nextItem = function () { //清除過分定時器 clearInterval(this.timer); //清除間隔播放定時器 clearInterval(this.autoStart); this.timer = setInterval(() => { let left = parseInt(this.room.style.left); if (this.imgIndex < this.imgs.length) { if (left > (-this.slideWidth * this.imgIndex)) { this.room.style.left = `${parseInt(this.room.style.left)+this.speed}px`; } else { clearInterval(this.timer); this.room.style.left = `${-this.slideWidth*this.imgIndex}px`; this.navButtons[this.imgIndex - 1].style.background = '#fff'; this.imgIndex++; this.navButtons[this.imgIndex - 1].style.background = '#333'; } } else { if (left < 0) { this.room.style.left = `${parseInt(this.room.style.left)+(-this.speed*2)}px`; } else { clearInterval(this.timer); this.room.left = '0'; this.navButtons[this.imgIndex - 1].style.background = '#fff'; this.imgIndex = 1; this.navButtons[this.imgIndex - 1].style.background = '#333'; } } }, 16.7); this.autoStart = setInterval(() => { this.nextItem() }, this.timeOut); } Slide.prototype.lastItem = function () { clearInterval(this.timer); clearInterval(this.autoStart); this.timer = setInterval(() => { let left = parseInt(this.room.style.left); if (this.imgIndex > 1) { if (left < (-this.slideWidth * (this.imgIndex - 2))) { this.room.style.left = `${parseInt(this.room.style.left)-this.speed}px`; } else { clearInterval(this.timer); this.room.style.left = `${-this.slideWidth*(this.imgIndex-2)}px`; this.navButtons[this.imgIndex - 1].style.background = '#fff'; this.imgIndex--; this.navButtons[this.imgIndex - 1].style.background = '#333'; } } else { if (left > -this.slideWidth * (this.imgs.length - 1)) { this.room.style.left = `${parseInt(this.room.style.left)+(this.speed*2)}px`; } else { clearInterval(this.timer); this.room.left = `${-this.slideWidth*this.imgs.length-1}px`; this.navButtons[this.imgIndex - 1].style.background = '#fff'; this.imgIndex = this.imgs.length; this.navButtons[this.imgIndex - 1].style.background = '#333'; } } }, 16.7); this.autoStart = setInterval(() => { this.nextItem() }, this.timeOut); } Slide.prototype.navButtonClick = function ({ target }) { clearInterval(this.autoStart); clearInterval(this.timer); this.timer = setInterval(() => { let left = parseInt(this.room.style.left); if (target.index > this.imgIndex) { if (left > -this.slideWidth * (target.index - 1)) { this.room.style.left = `${parseInt(this.room.style.left)+this.speed*(target.index-this.imgIndex)}px`; } else { clearInterval(this.timer); this.room.style.left = `${-(this.slideWidth)*(target.index-1)}px`; this.navButtonStyle(target); } } else if (target.index < this.imgIndex) { if (left < -this.slideWidth * (target.index - 1)) { this.room.style.left = `${parseInt(this.room.style.left) - this.speed * (this.imgIndex - target.index)}px`; } else { clearInterval(this.timer); this.room.style.left = `${(-this.slideWidth) * (target.index - 1)}px`; this.navButtonStyle(target); } } else { return false; } }, 16.7); this.autoStart = setInterval(() => { this.nextItem() }, this.timeOut); } //導航圓點點擊事件 Slide.prototype.navButtonStyle = function (target) { target.style.background = '#333'; this.navButtons[this.imgIndex - 1].style.background = '#fff'; this.imgIndex = target.index; } //手指觸摸起始觸發函數 Slide.prototype.touchstart = function (e = window.event) { this.startPoint = e.touches[0]; } //手指觸摸結束觸發函數 Slide.prototype.touchend = function (e = window.event) { let endPoint = e.changedTouches[0]; let x = endPoint.clientX - this.startPoint.clientX; let y = endPoint.clientY - this.startPoint.clientY; if (Math.abs(x) > this.d) { if (x < 0) { this.nextItem(); } else { this.lastItem(); } } } this.Slide = Slide; })();
使用說明以及源代碼後續的優化,歡迎關注個人github 插件地址 slide插件
歡迎評論以及留言,同時歡迎關注個人博客定時不斷地更新個人文章 陳建光的博客函數