0

I need to have some variable values change on window.resize().

I already found that you need to declare the variables outside the .resize function scope, i then try to change their values in the function .resize scope. But this doesn't seem to work.

    var windowHeight = $(window).height();
    var goPanel0, goPanel1, goPanel2, goPanel3;

        goPanel0 = 0;
        goPanel1 = windowHeight;
        goPanel2 = windowHeight * 2;
        goPanel3 = windowHeight * 3;

    $(window).resize(function(){

        goPanel1 = windowHeight;
        goPanel2 = windowHeight * 2;
        goPanel3 = windowHeight * 3;

        //alert(goPanel3);

    });

Thx,

1
  • Are you getting alert fired? in resize() Commented Jul 3, 2013 at 9:07

2 Answers 2

4
$(window).resize(function(){
        windowHeight = $(window).height(); // add this line
        goPanel1 = windowHeight;
        goPanel2 = windowHeight * 2;
        goPanel3 = windowHeight * 3;

        //alert(goPanel3);

    });  

value to variable windowHeight was assigned out of .resize scope, and in .resize has previous value, but should have new value

Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain why windowHeight is out of scope? its a global var.
@KevinBowersox answer is reformulated. English is not my native language)
2

simply you are not getting the new window height after resizing, so it will give you the same old value over and over, you have to re-assign the value (get the value) from inside the event's function to get the new one and use it.

$(window).resize(function(){
        windowHeight = $(window).height(); // get new height after change
        goPanel1 = windowHeight;
        goPanel2 = windowHeight * 2;
        goPanel3 = windowHeight * 3;
});

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.