函数(i)在这个jQuery代码中意味着什么?

cat*_*use 0 javascript parameters jquery function

这是一个通过图像旋转的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);
  });
}
Run Code Online (Sandbox Code Playgroud)

pim*_*vdb 5

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

  • 首先是指数
  • 第二是价值

所以,i这里是索引,因为它是第一个参数.越高i越低zIndex(这是公式归结为).结果,图像将从背景上的最后一个zIndex显示到前面的第一个,因为更高意味着元素将显示在具有更低的元素的前面zIndex.

因此,越高i,越低zIndex,越被推到背景.