Question

I'm trying to use JQuery Waypoints to do a colour-changing header. I have an array of objects showing sections of the one-page site with their relevant colours (see below):

var colours = new Array(
    {section: 'home', colour: '#000'},
    {section: 'video-audio', colour: '#EF6E7C'},
    {section: 'motion-graphics', colour: '#FBC57A'},
    {section: 'graphic-design', colour: '#FFF9A9'},
    {section: 'web-development', colour: '#AAD693'},
    {section: 'social-media', colour: '#6D8DC9'},   
    {section: 'photography', colour: '#8A6FB0'},    
    {section: 'talent', colour: '#C57AB3'},     
    {section: 'contact', colour: '#000'}
);

I am then using the script below to create JQuery Waypoints for each section (which are marked up as <section id="..."> in the HTML):

// top bar whose background-colour is being changed
var $topBar = $('.top-bar');

var offsetNum = 0, // pixels to offset waypoints
    animateTime = 300, // milliseconds to animate
    fallbackColour = '#333';

// len is number of sections (length of colours array)
var len = (typeof colours == 'undefined') ? 0 : colours.length;

if (len > 1) {
    for (var i = 0; i < len; i++) {
        var curColour = ('colour' in colours[i]) ? colours[i].colour : fallbackColour,
            curSection = ('section' in colours[i]) ? colours[i].section : 'unknown';

        $('#' + curSection).waypoint(function(direction) {
            if (i > 0 && i < len) {
                curColour = (direction === 'down') ?
                    (colours[i+1].hasOwnProperty('colour') ? colours[i+1].colour : fallbackColour) :
                    (curColour = colours[i-1].hasOwnProperty('colour') ? colours[i-1].colour : fallbackColour);
            }

            // first or last waypoint
            $topBar.stop().animate({backgroundColor: curColour}, animateTime);
        }, { offset: offsetNum });
    }
}

When I just manually created waypoints for each section instead of using an array of objects and a for loop, it worked, but this was a lot of code repetition. If more sections are added later, I want to do as little modification as possible.

What I found when debugging with alerts was that all the Waypoints are seeing i as 9 (the maximum it is going to be) - is there any way to pass the correct i value to each Waypoint directly, or is there a better way of solving this?

Was it helpful?

Solution

At the moment the callback is called, the var i has the value 9 (all the callbacks still have the same reference to i). So you should pass the actual value. For example:

for (var i = 0; i < len; i++) {

    var curSection = ('section' in colours[i]) ? colours[i].section : 'unknown'; 

    addWaypoint(i, curSection);
}

function addWaypoint( n, curSection ){

    // n now has the value 0, 1, 2, etc. instead of referencing i

    $('#' + curSection).waypoint(function(direction) {
        console.log(i, direction); // 0, "down"
    });    
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top