All the event listeners will have a reference to the same i
which will be incremented each time (untill the point where i
becomes the boundary 11
). So when the event listeners are called they will acess the value of that reference of i
they got which will be 11
for all of them. So scrollTop: $('.project-' + i+1).offset().top()
will be scrollTop: $('.project-' + 12).offset().top()
for all of the items (which I assume not an element) and thus the offset
will be undefined
.
You can use an IIFE (Imediately Invoked Function Expression) to create a separate closure for each iteration, thus the event listeners will have unique values like this:
for(var i = 0; i < 11; i++) {
(function(index) { // index will be created for each iteration (new one every time)
// instead of i use index
$('.project-' + index + '> .arrows > .arrow-bottom').click(function() {
$('html, body').animate({
scrollTop: $('.project-' + index + 1).offset().top()
}, 750);
});
})(i); // pass i to the call to initialize index of the IIFE
}