在window.resize上更改变量值 [英] Change variable values on window.resize

查看:131
本文介绍了在window.resize上更改变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在window.resize()上更改一些变量值。

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

我已经发现你需要在.resize函数范围之外声明变量,然后我尝试在函数.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,

推荐答案

只是在调整大小后你没有得到新的窗口高度,所以它会一遍又一遍地给你相同的旧值,你必须从事件的函数内部重新赋值(获取值)得到新的并使用它。

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;
});

这篇关于在window.resize上更改变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆