Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.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从1开始旋转递增计数器_Javascript_Jquery_Increment - Fatal编程技术网

Javascript从1开始旋转递增计数器

Javascript从1开始旋转递增计数器,javascript,jquery,increment,Javascript,Jquery,Increment,我有一个图像ID数组 var images = ['238239', '389943', '989238', ... ]; max = images.length; 数组索引显然从0开始。数组大小可以变化 比如说。如果数组中有5个图像,则索引为0、1、2、3、4 我通过增加当前索引来使用Jquery交换图像,如下所示: <script> currindex = 0 // Previous image, onclick event swap $("previous").c

我有一个图像ID数组

var images = ['238239', '389943', '989238', ... ];
max = images.length;
数组索引显然从0开始。数组大小可以变化

比如说。如果数组中有5个图像,则索引为0、1、2、3、4

我通过增加当前索引来使用Jquery交换图像,如下所示:

<script>

currindex = 0

// Previous image, onclick event swap
    $("previous").click(function(){
        currindex = (currindex+1) % max;   
        ...some more code to swap image...
    });


// Next image, onclick event swap
    $("next").click(function(){
    currindex = (currindex+max-1) % max;
    ...some more code to swap image...
}

</script>

currindex=0
//上一个图像,onclick事件交换
$(“上一个”)。单击(函数(){
currendex=(currendex+1)%max;
…更多用于交换图像的代码。。。
});
//下一个图像,onclick事件交换
$(“下一步”)。单击(函数(){
currendex=(currendex+max-1)%max;
…更多用于交换图像的代码。。。
}
这允许图像旋转,并在用户单击第4个索引上的“下一步”时在索引0处再次开始。使用“上一步”旋转也同样适用

我想显示当前位置的计数器,如下所示:

<div>Image 3 of 5</div>
图3/5
如何实现始终从索引0处的1开始并双向旋转的计数器

function updateCounter(){
   document.getElementById("counter").innerHTML = "Image "+(currindex+1)+" of "+max;
}

在代码中添加一些id为“counter”的元素,并将函数
updateCounter()
添加到两个单击事件中。

您的问题没有真正意义,特别是:“始终从索引0的1开始”。为什么不在索引中添加1以显示“Y的图像X”文本?
$('#someID')。文本(currendex+1);