1

I'm not understanding something about variables in javascript. I am trying to change/calculate "offset" (with the variable "theOffset") either before the localScroll function occurs, or more preferably when you resize the window. None of the instances below work, accept for the "//initialize offset".

How do I get the variable "theOffset" inside "$.localScroll" to change?

jQuery(function( $ ){

    //initialize offset
    var windowWidth = $(window).width();
    if (windowWidth < 900) {
        theOffset = 0;
    } else {
        theOffset = ($(window).width() - 900) / -2;
    }

$(window).resize(function() {
    //calculate offset
    var windowWidth = $(window).width();
    if (windowWidth < 900) {
        theOffset = 0;
    } else {
        theOffset = ($(window).width() - 900) / -2;
    }
});


    $.localScroll({
        target: '#content',
        queue:true,
        duration:1500,
        hash:true,
        stop:true,
        onBefore:function( e, anchor, $target ){
            //calculate offset
            var windowWidth = $(window).width();
            if (windowWidth < 900) {
                theOffset = 0;
            } else {
                theOffset = ($(window).width() - 900) / -2;
            }
        },
        offset: {top:0, left: theOffset,
        onAfter:function( anchor, settings ){
            if (windowWidth < 900) {
                theOffset = 0;
            } else {
                theOffset = ($(window).width() - 900) / -2;
            }
        }   
    });
});

If need to know, I am centering a div container to the window with the offset in a fancy side scrolling website ;)

2
  • You got 3 errors on jslint.com Commented Nov 18, 2010 at 21:27
  • Even without the error, the same problem persists ... {top:0, left:theOffset} is evaluated when executed, so no amount of closures of variable scope will fix the behavior and make it dynamic without knowing/[ab]using the scroll-to API/internals or re-invoking the localScroll function. Commented Nov 18, 2010 at 22:53

4 Answers 4

1

You could declare a myOffset object that will be passed by reference to offset: You can also refine the almost quadruplicated function into a single named function:

var myOffset = {top: 0, left: theOffset};
function calcOffset() {
  var windowWidth = $(window).width();
  if (windowWidth < 900) {
    myOffset.left = 0;
  } else {
    myOffset.left = (windowWidth - 900) / -2;
  }
}
calcOffset();
// note leaving off the () will pass the function as a parameter instead of calling it
$(window).resize(calcOffset);

$.localScroll({
    target: '#content',
    queue: true,
    duration: 1500,
    hash: true,
    stop: true,
    onBefore: calcOffset,
    offset: myOffset,
    onAfter: calcOffset
});
Sign up to request clarification or add additional context in comments.

Comments

0

declare theOffset outside the jquery function


var theOffset = 0;

jquery( function ( $ ) {
} )

4 Comments

can you explain why so I can understand?
@Dave by doing this "theOffset" will gain global scope, and changing it in once place should be "visible" in all other places.
The problem is value of theOffset is passed.
If the localScroll function is getting loaded before my init.js file (which contains the above code), do I need to declare the variable before localScroll loads, or just before "jQuery(function( $ ){});" which is inside init.js (which loads after localScroll plugin).
0

I think your problem is here:

offset: {top:0, left: theOffset,

First off, you are missing a trailing }.

Secondly, you are doing what is called "passing by value" - the value that is sent when you call $.localscroll() is actually a copy of the value specified in theOffset.

Which version of localScroll are you using? I downloaded the latest source I could find, which was 1.2.7, and I see nothing about offset anywhere.

There is no immediate way I can think of addressing your problem without seeing the localScroll source. Sometimes authors of jQuery plugins like this provide a means of updating options. If that is not the case, you may have to modify the localScroll source to grab the offset value via a function callback instead of via an object property:

offset: {top:0, left: function() { return theOffset; } }

Note that the above will not work without modifying the localScroll source to get this data via a function call instead of options property.

Comments

0

Note that in the above example, if you call setupSomeGlobals() again, a new closure (stack-frame!) is created. The old gLogNumber, gIncreaseNumber, gSetNumber variables are overwritten with new functions that have the new closure. (In JavaScript, whenever you declare a function inside another function, the inside function(s) is/are recreated again each time the outside function is called.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.