Все, я ломаю себе голову дольше, чем хочу, чтобы признать, пытаясь полностью изменить эту функцию.У меня настроен цикл, поэтому мое слайд-шоу начинается при загрузке, останавливается при наведении курсора и может перемещаться с помощью кнопок «Далее» и «Предыдущий».Когда вы переключаете направления, слайды приходят в обоих направлениях и сбрасываются в одно.
Я почти уверен, что это связано с var bannerStatus
, являющимся глобальной переменной, и сбрасывается в единицу при смене направления.Есть ли способ сделать значение не сброшенным?Могу ли я использовать функцию для постоянного обновления глобальной переменной или я должен создать одну огромную функцию, которая работает в обе стороны?Вот мой banner.js для этого проекта.
var bannerStatus = 1;
var bannerTimer = 3000;
window.onload = function() {
bannerLoop();
}
var startBannerLoop = setInterval(function() {
bannerLoop();
}, bannerTimer);
document.getElementById("imgtabs").onmouseenter = function() {
clearInterval(startBannerLoop);
}
document.getElementById("imgtabs").onmouseleave = function() {
startBannerLoop = setInterval(function() {
bannerLoop();
}, bannerTimer);
}
function bannerLoop() {
if (bannerStatus === 1) {
document.getElementById("ban2").style.opacity = "0";
setTimeout(function() {
document.getElementById("ban3").style.right = "100%";
document.getElementById("ban3").style.zIndex = "900";
document.getElementById("ban1").style.right = "0px";
document.getElementById("ban1").style.zIndex = "1000";
document.getElementById("ban2").style.right = "-100%";
document.getElementById("ban2").style.zIndex = "1100";
}, 500);
setTimeout(function() {
document.getElementById("ban2").style.opacity = "1";
}, 1000);
bannerStatus++;
} else if (bannerStatus === 2) {
document.getElementById("ban3").style.opacity = "0";
setTimeout(function() {
document.getElementById("ban1").style.right = "100%";
document.getElementById("ban1").style.zIndex = "900";
document.getElementById("ban2").style.right = "0px";
document.getElementById("ban2").style.zIndex = "1000";
document.getElementById("ban3").style.right = "-100%";
document.getElementById("ban3").style.zIndex = "1100";
}, 500);
setTimeout(function() {
document.getElementById("ban3").style.opacity = "1";
}, 1000);
bannerStatus++;
} else if (bannerStatus === 3) {
document.getElementById("ban1").style.opacity = "0";
setTimeout(function() {
document.getElementById("ban2").style.right = "100%";
document.getElementById("ban2").style.zIndex = "900";
document.getElementById("ban3").style.right = "0px";
document.getElementById("ban3").style.zIndex = "1000";
document.getElementById("ban1").style.right = "-100%";
document.getElementById("ban1").style.zIndex = "1100";
}, 500);
setTimeout(function() {
document.getElementById("ban1").style.opacity = "1";
}, 1000);
bannerStatus = 1;
}
}
function reverseBanner() {
if (bannerStatus === 1) {
document.getElementById("ban3").style.opacity = "0";
setTimeout(function() {
document.getElementById("ban3").style.right = "100%";
document.getElementById("ban3").style.zIndex = "900";
document.getElementById("ban1").style.right = "0px";
document.getElementById("ban1").style.zIndex = "1000";
document.getElementById("ban2").style.right = "-100%";
document.getElementById("ban2").style.zIndex = "1100";
}, 500);
setTimeout(function() {
document.getElementById("ban3").style.opacity = "1";
}, 1000);
bannerStatus++;
} else if (bannerStatus === 2) {
document.getElementById("ban2").style.opacity = "0";
setTimeout(function() {
document.getElementById("ban2").style.right = "100%";
document.getElementById("ban2").style.zIndex = "900";
document.getElementById("ban3").style.right = "0px";
document.getElementById("ban3").style.zIndex = "1000";
document.getElementById("ban1").style.right = "-100%";
document.getElementById("ban1").style.zIndex = "1100";
}, 500);
setTimeout(function() {
document.getElementById("ban2").style.opacity = "1";
}, 1000);
bannerStatus++;
} else if (bannerStatus === 3) {
document.getElementById("ban1").style.opacity = "0";
setTimeout(function() {
document.getElementById("ban1").style.right = "100%";
document.getElementById("ban1").style.zIndex = "900";
document.getElementById("ban2").style.right = "0px";
document.getElementById("ban2").style.zIndex = "1000";
document.getElementById("ban3").style.right = "-100%";
document.getElementById("ban3").style.zIndex = "1100";
}, 500);
setTimeout(function() {
document.getElementById("ban1").style.opacity = "1";
}, 1000);
bannerStatus = 1;
}
document.getElementById("nxtbtn").onclick = function() {
bannerLoop();
}
document.getElementById("prvbtn").onclick = function() {
reverseBanner();
}
#imgtabs {
background-color: hsl(43, 0%, 93%);
height: 250px;
width: 100%;
overflow: hidden;
position: relative;
}
.tab {
height: 100%;
width: 100%;
position: absolute;
top: 0px;
transition: all ease-in-out 500ms;
}
#ban1 {
background-image: url(imgs/Romans%20Group.jpg);
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
#ban2 {
background-image: url(imgs/AlphaMailTruck.jpg);
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
#ban3 {
background-image: url(imgs/Romans%20Group.jpg);
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
.tab h3 {
position: absolute;
right: 5%;
bottom: 2%;
color: white;
}
.imgbtn {
height: 40px;
width: 40px;
position: absolute;
top: 50%;
z-index: 1200;
cursor: pointer;
}
.imgbtn:hover {
opacity: 0.9;
}
.prvbtn {
left: 5px;
}
.nxtbtn {
right: 5px;
}
<html>
<body>
<div id="imgtabs">
<div class="tab" id="ban1">
<h3>First</h3>
</div>
<div class="tab" id="ban2">
<h3>Second</h3>
</div>
<div class="tab" id="ban3">
<h3>Third</h3>
</div>
<a class="imgbtn prvbtn" id="prvbtn">❮</a>
<a class="imgbtn nxtbtn" id="nxtbtn">❯</a>
</div>
</body>
</html>