JQuery: I don't understand what "i" is in .each (function (i)

I am trying to understand this jquery code, I understand everything: modulo, loop, etc., except that "i" means in .each (function (i). I tried searching, but this code does not return good search results Does it represent every img that has the identifier #photos? If it is not like the variable currentPhoto. Any help with understanding this or linking to useful information would be greatly appreciated.

function rotatePics(currentPhoto) { //number is index of current photo var numberOfPhotos = $('#photos img').length; currentPhoto = currentPhoto % numberOfPhotos; $('#photos img').eq(currentPhoto).fadeOut(function() { // re-order the z-index $('#photos img').each(function(i) { $(this).css( 'zIndex', ((numberOfPhotos - i) + currentPhoto) % numberOfPhotos ); }); $(this).show(); setTimeout(function() {rotatePics(++currentPhoto);}, 4000); }); } 

Thanks in advance!

Edit: In case someone goes through jquery: a newcomer to the ninja book has the same doubts: (i) is based on 0 and currentPhoto is on 1, so for example, the 1st img element will look like this (when provided that only 6 photos):

 ((6 - 0) + 1 % 6 ((numberOfPhotos - i) + currentPhoto) % numberOfPhotos 

always adding 1 ensures that the remainder is always 1, so make sure the z-index is always 1.

Thanks for helping all the people!

+4
source share
4 answers

i - index position of the current element in the jquery collection

+7
source

Each function () is registered here http://api.jquery.com/each/

With jquery selectors, you will have a list of objects, and I will be the index of these elements in the order that dom was created.

+4
source

'i' should represent the index inside the loop.

+3
source

Source: https://habr.com/ru/post/1347178/


All Articles