最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change variable values on window.resize - Stack Overflow

programmeradmin6浏览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,

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,

Share Improve this question asked Jul 3, 2013 at 9:06 keviniuskevinius 4,6287 gold badges51 silver badges79 bronze badges 1
  • Are you getting alert fired? in resize() – Dhaval Marthak Commented Jul 3, 2013 at 9:07
Add a ment  | 

2 Answers 2

Reset to default 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

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)

  1. 暂无评论