Dave
Dave

Reputation: 9157

Help me with javascript variable's scope

How do I get the variable "theOffset" to work inside the "localScroll" function. This is my non-working code:

jQuery(function( $ ){
    //initialize variable "theOffset"
    theOffset = 10; // <-- sets variable for inside of "localScroll" function

        $(window).resize(function() {
            theOffset = 20; // <-- does NOT reset inside "localScroll" function
        }); // end of window resize


    $.localScroll({
            target: '#content', 
            duration:1500,
            hash:true,
            offset: {top:0, left: theOffset}, // <-- I want to reset this on window resize 
            onBefore:function( e, anchor, $target ){
            },
            onAfter:function( anchor, settings ){
            }
    });
});

Upvotes: 0

Views: 415

Answers (1)

aepheus
aepheus

Reputation: 8187

A couple of pointers:

Your creating objects with all those {}. Though it may look like simple functions, they aren't.

offset is a property of the object being passed to $.localScroll

One thought is to make the object (which is passed to $.localScroll) outside of $.localScroll, set it to a variable, and reference that variable.

Not sure if this will work as is, may have to jump through a hoop or two still.

Something along the lines of:

var theOffset = {
...
offset: {top:0, left: whatever},
...
};

$(window).resize(function() {
theOffset.offset = 20;
});

$.localScroll(theOffset);

Upvotes: 1

Related Questions