我正在制作一个使用JSON数据填充其内容的幻灯片。当我来到最后一张幻灯片时,我必须单击2-3次,然后幻灯片才能在第一张幻灯片上再次开始。我要确保不会发生这种情况,只需单击一下就可以再次从第一张幻灯片开始。一个类似的错误是,当我第一次加载页面并第一次单击“后退”按钮时,我必须单击两次,此后它应能正常工作。我确定我的一个if语句中有一个问题,但我无法弄清楚出了什么问题。
index.html
<div class="slideshow-container">
<div class="mySlides fade">
<img id="image" style="width:100%" >
<div id="description"></div>
</div>
<a id="prev">❮</a>
<a id="next">❯</a>
</div>
<script>
window.onload = startup;
var xmlhttp;
function startup () {
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = statechange;
xmlhttp.open("GET", "slideshow.json", true);
xmlhttp.send();
}
function statechange() {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
var printout = " ";
var doc = JSON.parse(xmlhttp.responseText);
var amountOfElements = doc.length;
var current = 0;
var imagePath = "assets/stop2/";
document.getElementById("image").src = imagePath + doc[current].image;
document.getElementById("description").innerHTML = doc[current].text;
console.log(current);
prev.onclick = function() {
if(current !== 0){
current = current - 1;
document.getElementById("image").src = imagePath + doc[current].image;
document.getElementById("description").innerHTML = doc[current].text;
if(current === 0) {
current = amountOfElements;
}
}
else {
current = amountOfElements;
}
console.log(current);
};
next.onclick = function() {
if(current < amountOfElements) {
current = current + 1;
document.getElementById("image").src = imagePath + doc[current].image;
document.getElementById("description").innerHTML = doc[current].text;
}
else {
current = -1;
}
console.log(current);
};
}
else if (xmlhttp.readyState === 4 && xmlhttp.status === 404) {
alert("Something went wrong");
}
}
</script>
slideshow.json
{
"name": "First image",
"text": "blablabla",
"image": "img1.png",
"audio": "audio1.mp3"
},
{
"name": "Second image",
"text": "Lorem ipsum..",
"image": "img2.png",
"audio": "audio2.mp3"
},
{
"name": "Third image",
"text": "blablabla",
"image": "img3.png",
"audio": "audio3.mp3"
},
{
"name": "Last image",
"text": "blablablaaaaaa",
"image": "img4.png",
"audio": "audio4.mp3"
}
] ```
如果您想要无限循环行为,请尝试以下操作:
var updateUI = function() {
document.getElementById("image").src = imagePath + doc[current].image;
document.getElementById("description").innerHTML = doc[current].text;
}
prev.onclick = function() {
if(current <= 0) {
current = amountOfElements - 1; // index of last element
}
else {
current--; // just decrement
}
console.log(current);
updateUI();
};
next.onclick = function() {
if(current < amountOfElements - 1) {
current++; // just increment
}
else {
current = 0; // start at first element
}
console.log(current);
updateUI();
};
问题是,current = -1
和current = amountOfElements
超出范围,因此您需要递增或递减一到三次,直到索引变为“有效”。
您仅在if块而不是else块中进行更新。我将更新代码放在if / else语句之后。而且,您使用了元素数量,但是从0迭代,这是错误的。这就是为什么我将其更改为元素数量-1的原因。在3-> 0的情况下,您将步长更改为-1(不知道为什么?),而不是0。
prev.onclick = function() {
if (current !== 0) {
current = current - 1;
} else {
current = amountOfElements - 1;
}
document.getElementById("image").src = imagePath + doc[current].image;
document.getElementById("description").innerHTML = doc[current].text;
};
next.onclick = function() {
if (current < amountOfElements - 1) {
current = current + 1;
} else {
current = 0;
}
document.getElementById("image").src = imagePath + doc[current].image;
document.getElementById("description").innerHTML = doc[current].text;
};