admin管理员组文章数量:1320661
This is a sample code from a jQuery book that rotates through images. I understand most of it except for the part that says function(i). What value is being passed to (i) as an argument and when (i) is being subtracted to numberOfPhotos, what is exactly the value being subtracted?
$(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);
});
}
This is a sample code from a jQuery book that rotates through images. I understand most of it except for the part that says function(i). What value is being passed to (i) as an argument and when (i) is being subtracted to numberOfPhotos, what is exactly the value being subtracted?
$(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);
});
}
Share
Improve this question
asked Sep 2, 2011 at 9:41
catandmousecatandmouse
11.8k24 gold badges95 silver badges157 bronze badges
3 Answers
Reset to default 5The .each
function calls the function you pass (function(i) {...
here) and passes two variables in turn to that function:
- the first is the index
- the second is the value
So, i
is the index here, as it's the first argument. The higher i
, the lower zIndex
(this is what the formula boils down to). As a result, the images will be displayed from the last on the background to the first on the foregound, since a higher zIndex
means that the element will be displayed in front of an element with a lower zIndex
.
So, the higher i
, the lower zIndex
, the more it wil be pushed to the background.
'i' is the index in the array of the current item.
From jQuery 'each' docs -
callback(indexInArray, valueOfElement) The function that will be executed on every object.
When calling 'each' you could pass no aruments -
$('#photos img').each(function()
But if you do choose to pass arguments -
$('#photos img').each(function(index,val)
then jQuery will populate the value of each argument with the relevant values for each function call in the 'each' loop.
It's the index of the list of items where you are looping through. i is very mon variable name for that.
本文标签: javascriptWhat does function(i) mean in this jQuery codeStack Overflow
版权声明:本文标题:javascript - What does function(i) mean in this jQuery code? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742063050a2418673.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论