您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)JS如何實(shí)現(xiàn)多重選項(xiàng)卡切換輪播圖,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
輪播圖簡介:在一個(gè)網(wǎng)站的某一特定模塊,通過電腦上鼠標(biāo)點(diǎn)擊或鼠標(biāo)移入、手機(jī)上手指滑動(dòng)后,可以分別展示不同的圖片,這個(gè)模塊就叫做輪播模塊。
html布局部分:
<div id="box"> <div class="scenery pic"> <img class="show" src="imgs/s2.jpg" alt="scenery"> <img src="imgs/s3.jpg" alt="scenery"> <img src="imgs/s1.jpg" alt="scenery"> <img src="imgs/s5.jpg" alt="scenery"> <img src="imgs/s4.jpg" alt="scenery"> </div> <div class="car pic"> <img src="imgs/animal4.jpg" alt="animal"> <img src="imgs/animal3.jpg" alt="animal"> <img src="imgs/animal2.jpg" alt="animal"> <img src="imgs/animal1.jpg" alt="animal"> </div> <div class="entertainment pic"> <img src="imgs/entertainment1.jpg" alt="entertainment"> <img src="imgs/entertainment2.jpg" alt="entertainment"> <img src="imgs/entertainment3.jpg" alt="entertainment"> <img src="imgs/entertainment4.jpg" alt="entertainment"> <img src="imgs/entertainment5.jpg" alt="entertainment"> </div> <div class="leftbar"> <div class="checked">風(fēng)景</div> <div>名車</div> <div>娛樂</div> </div> <div class="bottombar"> </div> </div>
CSS樣式部分:
/* 為了布局方便,容器里大多采用的flex */ #box { position: relative; width: 460px; height: 300px; margin: 40px auto; border: 1px solid rgb(109, 98, 98); user-select: none; } /* 側(cè)邊欄布局 */ .leftbar { display: flex; flex-direction: column; justify-content: space-between; position: absolute; top: -1px; left: -80px; width: 80px; height: 100%; text-align: center; font-size: 20px; cursor: pointer; } .leftbar div { flex: 1; line-height: 100px; background-color: cadetblue; letter-spacing: 5px; } .leftbar div:nth-child(2) { border-top: 1px solid #fff; border-bottom: 1px solid #fff; } /* 底部切換按鈕樣式設(shè)計(jì) */ .bottombar { display: flex; justify-content: space-between; position: absolute; bottom: -1px; right: -1px; z-index: 10; width: 200px; height: 30px; cursor: pointer; } .bottombar div { flex: 1; line-height: 30px; background-color: rgb(232, 233, 235, .5); text-align: center; font-weight: 700; } .bottombar div~div { border-left: 1px solid grey; } img { position: absolute; display: block; width: 460px; height: 300px; } .show { z-index: 5; } .leftbar .checked, .bottombar .checked { background-color: rgb(241, 5, 5); }
javascript邏輯實(shí)現(xiàn)部分:
var Box = document.querySelector('#box'), pic = Box.querySelectorAll('.pic'), Idx = 0, index = 0, timer = null, ltDiv = Box.querySelector('.leftbar'), btDiv = Box.querySelector('.bottombar'), Img = Box.querySelectorAll('img'); function createBtBar(len) {//動(dòng)態(tài)創(chuàng)建底部切換按鈕 var str = ''; for (var i = 0; i < len; i++) { str += `<div>${i + 1}</div>`; } btDiv.innerHTML = str; btDiv.children[0].classList.add('checked'); } function initialize() {//頁面初始狀態(tài) createBtBar(pic[0].children.length); } initialize(); function clearZindex() {//重置所有圖片的定位層級 for (var k = 0; k < Img.length; k++) { Img[k].classList.remove('show'); } } ltDiv.addEventListener('click', (e) => {//側(cè)邊欄項(xiàng)目切換 if (e.target.tagName.toLowerCase() === 'div') { for (var j = 0; j < ltDiv.children.length; j++) { ltDiv.children[j].classList.remove('checked'); } clearZindex(); Idx = 0; index = getEleIdx(e.target); ltDiv.children[index].classList.add('checked'); pic[index].children[0].classList.add('show'); createBtBar(pic[index].children.length); } }); btDiv.addEventListener('click', (e) => {//委托監(jiān)聽底部切換按鈕 if (e.target.tagName.toLowerCase() === 'div') { function changePic(callback) { btDiv.children[Idx].classList.remove('checked'); clearZindex(); callback && callback(); btDiv.children[Idx].classList.add('checked'); pic[index].children[Idx].classList.add('show'); } changePic(function () { Idx = getEleIdx(e.target); }); } }); function getEleIdx(item) {//獲取DOM元素下標(biāo) var elements = item.parentNode.children; for (var i = 0, len = elements.length; i < len; i++) { if (item === elements[i]) { return i; } } } function loopShow() {//循環(huán)自動(dòng)展示 clearInterval(timer); timer = setInterval(function () { pic[index].children[Idx].classList.remove('show'); btDiv.children[Idx].classList.remove('checked'); Idx += 1; if (Idx < 0 || Idx > pic[index].children.length - 1) { Idx = 0; } pic[index].children[Idx].classList.add('show'); btDiv.children[Idx].classList.add('checked'); }, 1000); } loopShow(); Box.addEventListener('mouseover', function () { clearInterval(timer);//鼠標(biāo)移入展示區(qū)停止輪播 }); Box.addEventListener('mouseout', function () { loopShow();//鼠標(biāo)移出展示區(qū)自動(dòng)輪播 });
具體實(shí)現(xiàn)的展示效果入下:
關(guān)于“JS如何實(shí)現(xiàn)多重選項(xiàng)卡切換輪播圖”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。