$(window).resize(); doesn't work

后端 未结 5 1973
梦谈多话
梦谈多话 2021-01-28 07:21

I\'ve got a problem with window.resize . My code js/jquery is here

var x = $(window).width();
var y = $(window).height();
var z = $(\'#card\').height();
var a =          


        
5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-28 08:16

    Your final two lines seem to be the problem here:

    $(document).ready( updateBodySize() );
    $(window).resize( updateBodySize() );
    

    Should be:

    $(document).ready( updateBodySize );
    $(window).resize( updateBodySize );
    

    Note the dropping of the () from updateBodySize - your aim is to pass the function updateBodySize to .ready and .resize, not its result. By call the function instead, what you're doing is passing the result of updateBodySize() to the .ready and .resize functions, in effect:

    $(document).ready( null );
    $(window).resize( null );
    

    Which, as you've noticed, does nothing except what updateBodySize() does first (two) times you called it. Drop the () and it will be treated as the event handler you expect.

    PS:

    Unless you're using the first block of

    var x = $(window).width();
    var y = $(window).height();
    var z = $('#card').height();
    var a = z + 140;
    var b = 1.78 * y;
    var c = 1.78 * a;
    

    before your function block, you can drop it, since you redefine those var inside the function, so it'll calculate them independantly any time it's called.

提交回复
热议问题