let front = document.querySelector('.front') let back = document.querySelector('.back') let nextPage = front.onclick = function () { let switchList = document.querySelector('.switchList') currentLeft = switchList.offsetLeft switchList.style.left = -mainwidth + currentLeft + 'px' if (currentLeft == -mainwidth * 4) { switchList.style.left = '0px' } flowMove(setpos) } back.onclick = function () { let switchList = document.querySelector('.switchList') currentLeft = switchList.offsetLeft switchList.style.left = mainwidth + currentLeft + 'px' if (currentLeft == 0) { switchList.style.left = -mainwidth * 4 + 'px' } setpos = false flowMove(setpos) }
底部按钮与图片的关联导航
1 2 3 4 5 6 7
let buttonLists = document.querySelectorAll('.switchButtons>ul>li') let switchList = document.querySelector('.switchList') for (let i = 0; i < buttonLists.length; i++) { buttonLists[i].onclick = function () { switchList.style.left = -mainwidth * i + 'px' } }
点击对应底部按钮时,给予唯一显示的背景色以标识
1 2 3 4 5 6 7 8 9 10 11
let aLists = document.querySelectorAll('.switchButtons>ul>li>a') for (let i = 0; i < aLists.length; i++) { aLists[i].onclick = function () { for (let j = 0; j < aLists.length; j++) { if (aLists[j].hasAttribute('class')) { aLists[j].removeAttribute('class') } } this.setAttribute('class', 'active') } }
图片左右切换时,对应按钮随之捆绑流动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
functionflowMove() { if (setpos) { index = Math.abs(currentLeft / mainwidth) + 1 if (index > aLists.length - 1) { index = 0 } } else { index = Math.abs(currentLeft / mainwidth) - 1 if (index < 0) { index = aLists.length - 1 } } for (let i = 0; i < aLists.length; i++) { aLists[i].removeAttribute('class') } aLists[index].setAttribute('class', 'active') }
自动轮播
1 2 3 4 5 6
navPic.onmouseover = function () { clearInterval(clearclock) } navPic.onmouseout = function () { clearclock = setInterval(nextPage, 2000) }
window.onload=function(){ let navPic = document.querySelector('.nav_pic') let mainwidth = navPic.offsetWidth let setpos = true let clearclock = null // averge switchButtons , make it center in horizontally let buttonItem = document.querySelector('.switchButtons>ul>li').offsetWidth let buttonsLength = document.querySelectorAll('.switchButtons>ul>li').length let switchButtons = document.querySelector('.switchButtons') switchButtons.style.left = (mainwidth - buttonItem * buttonsLength) / 2 + 'px' //the picture move to left or right let front = document.querySelector('.front') let back = document.querySelector('.back') let nextPage = front.onclick = function () { let switchList = document.querySelector('.switchList') currentLeft = switchList.offsetLeft switchList.style.left = -mainwidth + currentLeft + 'px' if (currentLeft == -mainwidth * 4) { switchList.style.left = '0px' } flowMove(setpos) } back.onclick = function () { let switchList = document.querySelector('.switchList') currentLeft = switchList.offsetLeft switchList.style.left = mainwidth + currentLeft + 'px' if (currentLeft == 0) { switchList.style.left = -mainwidth * 4 + 'px' } setpos = false flowMove(setpos) } //auto play picture navPic.onmouseover = function () { clearInterval(clearclock) } navPic.onmouseout = function () { clearclock = setInterval(nextPage, 2000) } // click button jump to the corresponding picture let buttonLists = document.querySelectorAll('.switchButtons>ul>li') let switchList = document.querySelector('.switchList') for (let i = 0; i < buttonLists.length; i++) { buttonLists[i].onclick = function () { switchList.style.left = -mainwidth * i + 'px' } } // switch buttons and jsut show red let aLists = document.querySelectorAll('.switchButtons>ul>li>a') for (let i = 0; i < aLists.length; i++) { aLists[i].onclick = function () { for (let j = 0; j < aLists.length; j++) { if (aLists[j].hasAttribute('class')) { aLists[j].removeAttribute('class') } } this.setAttribute('class', 'active') } } // button show red with move of the picture functionflowMove() { if (setpos) { index = Math.abs(currentLeft / mainwidth) + 1 if (index > aLists.length - 1) { index = 0 } } else { index = Math.abs(currentLeft / mainwidth) - 1 if (index < 0) { index = aLists.length - 1 } } for (let i = 0; i < aLists.length; i++) { aLists[i].removeAttribute('class') } aLists[index].setAttribute('class', 'active') } }