幻灯片放映按钮仅在进入最后一张幻灯片时需要第二次点击才能使用
Slideshow buttons only working on second click when coming to the last slide
我正在制作一个使用 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
超出范围,因此您需要递增和递减一到三次,直到索引达到 "valid"。
您只在 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;
};
我正在制作一个使用 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
超出范围,因此您需要递增和递减一到三次,直到索引达到 "valid"。
您只在 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;
};