Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将上一次/下一次切换添加到onclick图像库_Javascript_Arrays_Onclick_Toggle_Image Gallery - Fatal编程技术网

Javascript 将上一次/下一次切换添加到onclick图像库

Javascript 将上一次/下一次切换添加到onclick图像库,javascript,arrays,onclick,toggle,image-gallery,Javascript,Arrays,Onclick,Toggle,Image Gallery,我有几个onclick图像库,当用户单击图像时,下一个图像将在其中加载。这不是很明显,我想添加“下一步”和“上一步”切换。 我读过很多关于它的文章,但我对javascript不够熟悉,无法将一般建议应用到我的特定代码中(我不完全理解) 因此,我想获得关于如何编辑javascript函数的具体建议 这就是功能: function ImageCollection(images) { this.images = images; this.i = 0; this.next

我有几个onclick图像库,当用户单击图像时,下一个图像将在其中加载。这不是很明显,我想添加“下一步”和“上一步”切换。

我读过很多关于它的文章,但我对javascript不够熟悉,无法将一般建议应用到我的特定代码中(我不完全理解)

因此,我想获得关于如何编辑javascript函数的具体建议

这就是功能:

 function ImageCollection(images) {
     this.images = images;
     this.i = 0;
     this.next = function(img) {
     this.i++;
     if (this.i == images.length)
         this.i = 0;
         img.src = images[this.i];
     }
 }
以图像阵列为例:

var ic1 = new ImageCollection(['icono/antithese/preview.jpg','icono/antithese/preview2.jpg','icono/antithese/preview2.jpg',}
我在html中这样称呼它:

此外,它只有一种方式,这对于onclick函数很好,但我仍然想学习如何在使用切换时使其来回移动

谢谢你的帮助和关注 (我希望我的措辞清晰,如果不清楚,请询问我,我会尽量更精确!)

函数图像采集(图像){
这个。图像=图像;
这个。i=0;
this.next=函数(imgId){
var img=document.getElementById(imgId);
这个.i++;
if(this.i==images.length)
这个。i=0;
img.src=图像[this.i];
}
this.prev=函数(imgId){
var img=document.getElementById(imgId);
这个;

如果(this.i)您的代码中存在括号不一致,我不认为它们是不一致的,因为它们只是误导。有人会认为
img.src=images[this.i];
属于它前面的if语句块,但它不是。它前面的if语句的目的是在到达数组末尾时将变量重置为0,图像源的更改应该每次都会发生。哦,等等,我现在看到了。这现在是怎么工作的?我很惊讶它没有抛出语法错误。谢谢你,你的解释确实让我更清楚了!我很高兴它对我有帮助。如果你需要更多关于具体点的澄清,请提及它,我可以详细说明。
function ImageCollection(images) {
     this.images = images;
     this.i = 0;
     this.next = function(imgId) {
     var img = document.getElementById(imgId);
     this.i++;
     if (this.i == images.length )
         this.i = 0;
         img.src = images[this.i];
     }
     this.prev = function(imgId) {
     var img = document.getElementById(imgId);
     this.i--;
     if (this.i <= 0)
         this.i = images.length -1;
         img.src = images[this.i];
     }
 }
<img id='container' src="icono/antithese/preview.jpg" >
<input type='button' value='next' onclick='ic1.next("container")' />
<input type='button' value='prev' onclick='ic1.prev("container")' />