Change variable values on window.resize

后端 未结 2 459
执念已碎
执念已碎 2020-12-22 07:38

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

相关标签:
2条回答
  • 2020-12-22 08:03

    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;
    });
    
    0 讨论(0)
  • 2020-12-22 08:06
    $(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

    0 讨论(0)
提交回复
热议问题