在jquery中使用条件和依赖项来减少和简化代码?

在jquery中使用条件和依赖项来减少和简化代码?,jquery,Jquery,有一个滑块中使用的图像列表。一个接一个处于活动状态且可见。还有一个点击计数器,和滑块无关。在第10张图像上,然后在每一块5张图像之后,只有当clickcounter具有特定值时,才会发生一些事情。在这种情况下,应在以下情况下发生: 10th image and clickcounters value is 1 15th image and clickcounters value is 2 20th image and clickcounters value is 3 25th image and

有一个滑块中使用的图像列表。一个接一个处于活动状态且可见。还有一个点击计数器,和滑块无关。在第10张图像上,然后在每一块5张图像之后,只有当clickcounter具有特定值时,才会发生一些事情。在这种情况下,应在以下情况下发生:

10th image and clickcounters value is 1
15th image and clickcounters value is 2
20th image and clickcounters value is 3
25th image and clickcounters value is 4
... and so on. 
我的jquery看起来像这样。这些图像被编入索引:

   var num = 5;
   if (instance.index === num*2 && countCalc == 1 ) {
   do something;
   }
   else if (instance.index === num*3 && countCalc == 2 ) {
   do something;
   }
   else if (instance.index === num*4 && countCalc == 3 ) {
  do something;
   };
如果变量num ist设置为5,则与clickcounter存在明确的关系。clickcounter的值始终与第二个乘法器的值减去1相同。我不知道这应该如何写在一个句子中,而不是每5张图片重复一行。谁能给我一个提示吗?THX

您可以使用操作员:

var num = 5;
if (instance.index % num === 0) {
    doSomething(countCalc);
}

示例:

您可能需要尝试模块化除法:

if(instance.index%num==0&&countCalc==instance.index/num-1){}


instance
的索引除以num,如果所述索引是num的倍数,则余数为0。同样,该索引除以num,然后结果减去1得到
countCalc
值。

我怀疑这是一条一行的线,但请尝试深入研究以下内容:

$("div:nth-of-type(5)")
    .each(function(i, ele) {
        $(ele)
            .data("counter", i)
            .click(function(){
                if ($(this).data("counter") == counter) {
                    // do something
                }
            });
    });


不确定你的图片上是否已经有点击事件。所以我在这里分配和测试。也许是这个和@mhu建议的混合体

你就是达普萨曼国王!这似乎工作完美。我现在要稍微讨论一下模除法,来真正理解它。也要感谢所有其他人。