Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.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 在这个jQuery代码中,函数(i)是什么意思?_Javascript_Jquery_Function_Parameters - Fatal编程技术网

Javascript 在这个jQuery代码中,函数(i)是什么意思?

Javascript 在这个jQuery代码中,函数(i)是什么意思?,javascript,jquery,function,parameters,Javascript,Jquery,Function,Parameters,这是一个来自jQuery书籍的示例代码,该书在图像中旋转。除了说函数(I)的部分外,我了解大部分内容。作为参数传递给(i)的值是什么?当(i)被减去numberOfPhotos时,减去的值到底是什么 $(document).ready(function(){ rotatePics(1); }); function rotatePics(currentPhoto) { var numberOfPhotos = $('#photos img').length; currentPhot

这是一个来自jQuery书籍的示例代码,该书在图像中旋转。除了说函数(I)的部分外,我了解大部分内容。作为参数传递给(i)的值是什么?当(i)被减去numberOfPhotos时,减去的值到底是什么

$(document).ready(function(){
  rotatePics(1);
});

function rotatePics(currentPhoto) {
  var numberOfPhotos = $('#photos img').length; 
  currentPhoto = currentPhoto % numberOfPhotos;

  $('#photos img').eq(currentPhoto).fadeOut(function() { 

    $('#photos img').each(function(i) {
      $(this).css(
        'zIndex', ((numberOfPhotos - i) + currentPhoto) % numberOfPhotos
      );
    });
    $(this).show();
    setTimeout(function() {rotatePics(++currentPhoto);}, 4000);
  });
}

它是你循环浏览的项目列表的索引。i是非常常见的变量名。

'i'是当前项数组中的索引

从jQuery“每个”文档-

回调(indexInArray,ValueOfeElement)将调用的函数 在每个对象上执行

打电话给“每个人”时,你不能通过任何警告-

$('#photos img').each(function()
但如果你选择传递参数-

$('#photos img').each(function(index,val)
然后,jQuery将使用“each”循环中每个函数调用的相关值填充每个参数的值。

该函数调用您传递的函数(
function(i){…
此处),并将两个变量依次传递给该函数:

  • 第一个是索引
  • 第二个是价值
因此,
i
是这里的索引,因为它是第一个参数。越高的
i
,越低的
zIndex
(这就是公式的结论)。因此,图像将从背景的最后一个显示到前段的第一个,因为较高的
zIndex
表示元素将显示在具有较低
zIndex
的元素前面

因此,越高的
i
,越低的
zIndex
,它被推到后台的次数就越多