JavaScript實(shí)現(xiàn)輪播圖方法(邏輯清晰一看就懂)
輪播圖有很多種實(shí)現(xiàn)方法,這是其中一種最清晰的方法。思路很清晰,代碼很簡單,歡迎大佬多指教。
效果圖
先來看下效果圖,嫌麻煩就不用具體圖片來實(shí)現(xiàn)了,主要是理清思路。(自動(dòng)輪播,左右按鈕切換圖片,小圓點(diǎn)切換圖片,鼠標(biāo)移入暫停輪播,鼠標(biāo)移出繼續(xù)輪播)
HTML
首先是html內(nèi)容,布局很簡單,一個(gè)圖片列表,一個(gè)點(diǎn)列表,兩個(gè)按鈕。注意data-index使用HTML5中的data-xx屬性來嵌入自定義數(shù)據(jù)(下面JS代碼會(huì)提到)。記得給默認(rèn)顯示的圖片和對(duì)應(yīng)的小圓點(diǎn)加上active類哦。
<div class="wrap"> <ul class="list"> <li class="item active">0</li> <li class="item">1</li> <li class="item">2</li> <li class="item">3</li> <li class="item">4</li> </ul> <ul class="pointList"> <li class="point active" data-index = 0></li> <li class="point" data-index = 1></li> <li class="point" data-index = 2></li> <li class="point" data-index = 3></li> <li class="point" data-index = 4></li> </ul> <button class="btn" id="leftBtn"> < </button> <button class="btn" id="rightBtn"> > </button> </div>
CSS
思路:父容器list相對(duì)定位,item絕對(duì)定位,實(shí)現(xiàn)讓所有圖片重疊在父容器內(nèi)。利用z-index來設(shè)置圖片高度,圖片高度最高會(huì)顯示在頂層上。那么整個(gè)容器中,左右切換按鈕和小圓點(diǎn)始終要在最頂層,不能被圖片覆蓋,所以他們的z-index一定要比圖片高。active是一個(gè)樣式,給某張圖片綁定這個(gè)樣式就能在最上層顯示。然后就是圖片切換的漸變效果,opacity完全不透明到透明,再加個(gè)transition動(dòng)畫效果。最后就是cursor給小圓點(diǎn)添加“小手指”,其他就沒什么好說的了。
<style> .wrap { width: 800px; height: 400px; position: relative; } .list { width: 800px; height: 400px; position: relative; padding-left: 0px; } .item { width: 100%; height: 100%; list-style: none; position: absolute; left: 0; opacity: 0; transition: all .8s; } .item:nth-child(1) { background-color: skyblue; } .item:nth-child(2) { background-color: yellowgreen; } .item:nth-child(3) { background-color: rebeccapurple; } .item:nth-child(4) { background-color: pink; } .item:nth-child(5) { background-color: orange; } .item.active { z-index: 10; opacity: 1; } .btn { width: 60px; height: 100px; z-index: 100; top: 150px; position: absolute; } #leftBtn { left: 0px; } #rightBtn { right: 0px; } .pointList { list-style: none; padding-left: 0px; position: absolute; right: 20px; bottom: 20px; z-index: 200; } .point { width: 10px; height: 10px; background-color: antiquewhite; border-radius: 100%; float: left; margin-right: 8px; border-style: solid; border-width: 2px; border-color: slategray; cursor: pointer; } .point.active{ background-color: cadetblue; } </style>
Javascript
Index可以說是整個(gè)代碼里面的"核心",先封裝一個(gè)清除active的方法,這里面要清除圖片的active(顯示在最上層),比如切換到下一張圖上張圖的active就要清除。還有point的active(圖片對(duì)應(yīng)小圓點(diǎn)改變樣式)。然后goIndex這個(gè)方法就是給圖片和對(duì)應(yīng)的小圓點(diǎn)同時(shí)加上active,左右按鈕綁定的方法就不說了。
用getAttribute拿到剛才給li標(biāo)簽綁定的data-index屬性,綁定圖片index = pointindex,就能實(shí)現(xiàn)點(diǎn)擊小圓點(diǎn)圖片切換了。由于上面goIndex方法早已經(jīng)綁定好了給圖片添加active樣式的時(shí)候也給小圓點(diǎn)添加的樣式了,就可以實(shí)現(xiàn)圖片切換小圓點(diǎn)跟隨變化的效果。
<script> var items = document.querySelectorAll(".item");//圖片節(jié)點(diǎn) var points = document.querySelectorAll(".point")//點(diǎn) var left = document.getElementById("leftBtn"); var right = document.getElementById("rightBtn"); var all = document.querySelector(".wrap") var index = 0; var time = 0;//定時(shí)器跳轉(zhuǎn)參數(shù)初始化 //封裝一個(gè)清除active方法 var clearActive = function () { for (i = 0; i < items.length; i++) { items[i].className = 'item'; } for (j = 0; j < points.length; j++) { points[j].className = 'point'; } } //改變active方法 var goIndex = function () { clearActive(); items[index].className = 'item active'; points[index].className = 'point active' } //左按鈕事件 var goLeft = function () { if (index == 0) { index = 4; } else { index--; } goIndex(); } //右按鈕事件 var goRight = function () { if (index < 4) { index++; } else { index = 0; } goIndex(); } //綁定點(diǎn)擊事件監(jiān)聽 left.addEventListener('click', function () { goLeft(); time = 0;//計(jì)時(shí)器跳轉(zhuǎn)清零 }) right.addEventListener('click', function () { goRight(); time = 0;//計(jì)時(shí)器跳轉(zhuǎn)清零 }) for(i = 0;i < points.length;i++){ points[i].addEventListener('click',function(){ var pointIndex = this.getAttribute('data-index') index = pointIndex; goIndex(); time = 0;//計(jì)時(shí)器跳轉(zhuǎn)清零 }) } //計(jì)時(shí)器輪播效果 var timer; function play(){ timer = setInterval(() => { time ++; if(time == 20 ){ goRight(); time = 0; } },100) } play(); //移入清除計(jì)時(shí)器 all.onmousemove = function(){ clearInterval(timer) } //移出啟動(dòng)計(jì)時(shí)器 all.onmouseleave = function(){ play(); } </script>
總結(jié):這個(gè)簡單的輪播圖實(shí)現(xiàn)例子是第一次寫最容易理解,邏輯最清晰的一種。下面我把完整的代碼塊放出來,直接復(fù)制粘貼就可以運(yùn)行。
<!DOCTYPE html> <html> <head> <style> .wrap { width: 800px; height: 400px; position: relative; } .list { width: 800px; height: 400px; position: relative; padding-left: 0px; } .item { width: 100%; height: 100%; list-style: none; position: absolute; left: 0; opacity: 0; transition: all .8s; } .item:nth-child(1) { background-color: skyblue; } .item:nth-child(2) { background-color: yellowgreen; } .item:nth-child(3) { background-color: rebeccapurple; } .item:nth-child(4) { background-color: pink; } .item:nth-child(5) { background-color: orange; } .item.active { z-index: 10; opacity: 1; } .btn { width: 60px; height: 100px; z-index: 100; top: 150px; position: absolute; } #leftBtn { left: 0px; } #rightBtn { right: 0px; } .pointList { list-style: none; padding-left: 0px; position: absolute; right: 20px; bottom: 20px; z-index: 200; } .point { width: 10px; height: 10px; background-color: antiquewhite; border-radius: 100%; float: left; margin-right: 8px; border-style: solid; border-width: 2px; border-color: slategray; cursor: pointer; } .point.active{ background-color: cadetblue; } </style> </head> <body> <div class="wrap"> <ul class="list"> <li class="item active">0</li> <li class="item">1</li> <li class="item">2</li> <li class="item">3</li> <li class="item">4</li> </ul> <ul class="pointList"> <li class="point active" data-index = 0></li> <li class="point" data-index = 1></li> <li class="point" data-index = 2></li> <li class="point" data-index = 3></li> <li class="point" data-index = 4></li> </ul> <button class="btn" id="leftBtn"> < </button> <button class="btn" id="rightBtn"> > </button> </div> <script> var items = document.querySelectorAll(".item");//圖片 var points = document.querySelectorAll(".point")//點(diǎn) var left = document.getElementById("leftBtn"); var right = document.getElementById("rightBtn"); var all = document.querySelector(".wrap") var index = 0; var time = 0;//定時(shí)器跳轉(zhuǎn)參數(shù)初始化 //清除active方法 var clearActive = function () { for (i = 0; i < items.length; i++) { items[i].className = 'item'; } for (j = 0; j < points.length; j++) { points[j].className = 'point'; } } //改變active方法 var goIndex = function () { clearActive(); items[index].className = 'item active'; points[index].className = 'point active' } //左按鈕事件 var goLeft = function () { if (index == 0) { index = 4; } else { index--; } goIndex(); } //右按鈕事件 var goRight = function () { if (index < 4) { index++; } else { index = 0; } goIndex(); } //綁定點(diǎn)擊事件監(jiān)聽 left.addEventListener('click', function () { goLeft(); time = 0;//計(jì)時(shí)器跳轉(zhuǎn)清零 }) right.addEventListener('click', function () { goRight(); time = 0;//計(jì)時(shí)器跳轉(zhuǎn)清零 }) for(i = 0;i < points.length;i++){ points[i].addEventListener('click',function(){ var pointIndex = this.getAttribute('data-index') index = pointIndex; goIndex(); time = 0;//計(jì)時(shí)器跳轉(zhuǎn)清零 }) } //計(jì)時(shí)器 var timer; function play(){ timer = setInterval(() => { time ++; if(time == 20 ){ goRight(); time = 0; } },100) } play(); //移入清除計(jì)時(shí)器 all.onmousemove = function(){ clearInterval(timer) } //移出啟動(dòng)計(jì)時(shí)器 all.onmouseleave = function(){ play(); } </script> </body> </html>
總結(jié)
到此這篇關(guān)于JavaScript實(shí)現(xiàn)輪播圖方法的文章就介紹到這了,更多相關(guān)JS實(shí)現(xiàn)輪播圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ES6基礎(chǔ)之展開語法(Spread syntax)
這篇文章主要介紹了ES6基礎(chǔ)之展開語法(Spread syntax),主要介紹了擴(kuò)展語法的使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02js canvas實(shí)現(xiàn)寫字動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了js canvas實(shí)現(xiàn)寫字動(dòng)畫效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11js實(shí)現(xiàn)適用于素材網(wǎng)站的黑色多級(jí)菜單導(dǎo)航條效果
這篇文章主要介紹了js實(shí)現(xiàn)適用于素材網(wǎng)站的黑色多級(jí)菜單導(dǎo)航條效果,涉及javascript鼠標(biāo)事件及頁面元素樣式的動(dòng)態(tài)切換技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08Javascript數(shù)組操作函數(shù)總結(jié)
這篇文章主要給大家匯總介紹了Javascript數(shù)組操作函數(shù),需要的朋友可以參考下2015-02-02javascript實(shí)現(xiàn)簡單的二級(jí)聯(lián)動(dòng)
這篇文章主要介紹了javascript實(shí)現(xiàn)簡單的二級(jí)聯(lián)動(dòng),非常的實(shí)用,需要的朋友可以參考下2015-03-03JavaScript實(shí)現(xiàn)LRU算法的示例詳解
不知道屏幕前的朋友們,有沒有和我一樣,覺得LRU算法原理很容易理解,實(shí)現(xiàn)起來卻很復(fù)雜。所以本文就為大家整理了一下實(shí)現(xiàn)的示例代碼,需要的可以參考一下2023-04-04