简体   繁体   English

为什么这些函数不能用于在数组中显示我的图像?

[英]Why aren't the functions working for displaying my images in an array?

I'm a beginner with JavaScript and I cant seem to get my code to work.我是 JavaScript 的初学者,我似乎无法让我的代码工作。 I have this assignment that's long overdue and I cannot seem to get my goForward() ">>>" and goBackward() "<<<" functions to work properly.我有这个早就应该完成的任务,我似乎无法让 goForward() ">>>" 和 goBackward() "<<<" 函数正常工作。 It's supposed to start at the index and each time you click the ">>>" button, it'll show the next image in the array.它应该从索引开始,每次单击“>>>”按钮时,它都会显示数组中的下一个图像。 Same for the "<<<" button. “<<<”按钮相同。 I have 5 images in my array, and the ">>>" is showing 4 images then going back to the 1st image.我的阵列中有 5 张图像,“>>>”显示 4 张图像,然后返回到第 1 张图像。 The "<<<" is just broken. “<<<”只是坏了。 Sometimes it'll show the previous image and then skip to the 5th image.有时它会显示上一张图片,然后跳到第 5 张图片。 After that, it wont work.在那之后,它就行不通了。 Something is broken and I can't figure it out.有些东西坏了,我无法弄清楚。 Please help!!请帮忙!! Here is my code https://codepen.io/oshitaiya/pen/mddNWaq enter code here .这是我的代码https://codepen.io/oshitaiya/pen/mddNWaq enter code here Here is my assignment requirements https://docs.google.com/document/d/12IcETUGbgxC4jKBs0BK8HHLrjB_35mVlqK1kGTVIg-Y/edit?usp=sharing这是我的作业要求https://docs.google.com/document/d/12IcETUGbgxC4jKBs0BK8HHLrjB_35mVlqK1kGTVIg-Y/edit?usp=sharing

By keeping what you have.通过保留你所拥有的。 Running all the logic BEFORE appending.在追加之前运行所有逻辑。

var kawaii = ["https://media.giphy.com/media/3oKIPy2hjnheNraWwo/giphy.gif",
"https://media1.giphy.com/media/l3UcgEIw80eyb36kU/source.gif",
"https://media1.giphy.com/media/xTiN0mN3wxWO0MDuGk/source.gif",
"https://media2.giphy.com/media/3o6EhPDMjaDP5UC4ZW/source.gif",
"https://66.media.tumblr.com/3b9394556e6e0c6271594be2339c5d21/tumblr_nqkmdcPzbd1qak30bo1_500.gifv"]

var index = 0

function goForward(){
    index++
    if (index > kawaii.length - 1) {
         index = 0
    }
    document.getElementById("imgid").src = kawaii[index]
}

function goBackward(){
    index--
    if (index < 0) {
        index = kawaii.length - 1
    }
    document.getElementById("imgid").src = kawaii[index]
}

I've edited your code somewhere by replacing the two functions with changeImage function. 我已经通过将两个函数替换为changeImage函数来编辑了您的代码。

 var kawaii = ["https://media.giphy.com/media/3oKIPy2hjnheNraWwo/giphy.gif", "https://media1.giphy.com/media/l3UcgEIw80eyb36kU/source.gif", "https://media1.giphy.com/media/xTiN0mN3wxWO0MDuGk/source.gif", "https://media2.giphy.com/media/3o6EhPDMjaDP5UC4ZW/source.gif", "https://66.media.tumblr.com/3b9394556e6e0c6271594be2339c5d21/tumblr_nqkmdcPzbd1qak30bo1_500.gifv"] var index = 0 function changeImage() { index = index < 0 ? kawaii.length - 1 : index; index = index === kawaii.length - 1 ? 0 : index; document.getElementById("imgid").src = kawaii[index]; index++; }; 
 body { background-color: #a3d5d3; } 
 <h1>Image Gallery</h1> <img id="imgid"/><br> <input type="button" onclick="changeImage()" value="<<<<"/> <input type="button" onclick="changeImage()" value=">>>>"/> 


Or if you want to keep the two functions: 或者,如果您想保留两个功能:

 var kawaii = ["https://media.giphy.com/media/3oKIPy2hjnheNraWwo/giphy.gif", "https://media1.giphy.com/media/l3UcgEIw80eyb36kU/source.gif", "https://media1.giphy.com/media/xTiN0mN3wxWO0MDuGk/source.gif", "https://media2.giphy.com/media/3o6EhPDMjaDP5UC4ZW/source.gif", "https://66.media.tumblr.com/3b9394556e6e0c6271594be2339c5d21/tumblr_nqkmdcPzbd1qak30bo1_500.gifv"] var index = 0 function changeImage() { index = index < 0 ? kawaii.length - 1 : index; index = index === kawaii.length - 1 ? 0 : index; document.getElementById("imgid").src = kawaii[index]; index++; }; function goBackward() { changeImage(); } function goForward() { changeImage(); } 
 body { background-color: #a3d5d3; } 
 <h1>Image Gallery</h1> <img id="imgid"/><br> <input type="button" onclick="goBackward()" value="<<<<"/> <input type="button" onclick="goForward()" value=">>>>"/> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM