1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| let index = 0; let timer = null; let wrapperContainer = document.querySelector(".wrapper-container"); let wrapper = document.querySelector(".wrapper"); let imgNum = wrapper.children[0].children.length; let conWidth = wrapperContainer.offsetWidth;
let indicatorList = document.querySelector(".indicator ul").children;
let picChange = function() { index = index === imgNum ? 0:index; index = index === -1 ? 3:index; wrapper.style.left = -index*conWidth+'px'; let active = document.querySelector(".active"); active.className = ''; indicatorList[index].className = 'active'; }
function autoPlay() { index++;
picChange();
timer = setTimeout(autoPlay ,2000); };
timer = setTimeout(autoPlay, 2000);
let stopAction = function () { clearTimeout(timer); } let restartAction = function () { timer = setTimeout(autoPlay ,2000); }
wrapperContainer.addEventListener('mouseenter', stopAction); wrapperContainer.addEventListener('mouseleave', restartAction);
let leftArrow = document.querySelector('.left-arrow'); let rightArrow = document.querySelector('.right-arrow');
leftArrow.addEventListener('click',()=>{ index--; picChange(); }) rightArrow.addEventListener('click',()=>{ index++; picChange(); })
for (let i = 0; i < indicatorList.length; i++) { indicatorList[i].addEventListener('mouseover', ()=>{ index = i; picChange(); }); }
|