I'm trying to assign some scrolling destinations for a menu for my one page application, the idea of this script is to prevent myself from repeating the same code over and over, but it's not quite working out the way I want it to.
Basically what happens is that destinations[i]
becomes undefined and therefore I cannot access .dest
of it. When I try just to console log destinations[i]
I do get the desired result, but when I then go to click the buttons I can't scroll to anything since nothing is defined as the .offset
value.
Why doesn't the value of destinations[i]
remain after the loop and what can I do to make this work as intended?
var destinations = [
{class: '.home', dest: $('html, body')},
{class: '.lunch', dest: $('#menu-wrapper')},
{class: '.catering', dest: $('#catering-wrapper')},
{class: '.renting', dest: $('#renting-wrapper')},
{class: '.karaoke', dest: $('#karaoke-wrapper')},
{class: '.contact', dest: $('#site-footer')}
],
destLength = destinations.length,
i;
for (i = 0; i < destLength; i++) {
$(destinations[i].class + ' a').on('click', function() {
$('html, body').stop().animate({
scrollTop: destinations[i].dest.offset().top
}, 1000);
});
}