var index = 0
var imgs = document.getElementsByClassName("img")
var box = document.querySelector(".box-img")
var left = document.querySelector(".button-left")
var right = document.querySelector(".button-right")
function position() {
box.style.left = (index * -100) + "%"
}
function timer() {
time = setInterval(() => {
index++
add()
desc()
position()
}, 3000)
}
function add() {
if (index >= imgs.length - 1) {
index = 0
}
else {
index++
}
}
function desc() {
if (index < 1) {
index = imgs.length - 1
}
else {
index--
}
}
left.addEventListener("click", () => {
desc()
position()
clearInterval(time)
timer()
})
right.addEventListener("click", () => {
add()
position()
clearInterval(time)
timer()
})
timer()
就是left和right两个按钮里面清除了计时器就不能够重新调用了