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

后端 未结 5 1974
梦谈多话
梦谈多话 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 07:58

    Try this:

    $(function(){updateBodySize();}); //kiedy zaladowany
    $(window).resize(function(){updateBodySize();}); 
    
    0 讨论(0)
  • 2021-01-28 08:00

    I think you have to use this, because the way you are doing it, the function is executed and the return value of the function is set as the callback function, which will not work:

    $(document).ready(function() {
        updateBodySize();
    }); //kiedy zaladowany
    $(window).resize(function() {
        updateBodySize();
    });  //kiedy zmiana rozmiaru
    
    0 讨论(0)
  • 2021-01-28 08:15

    Or you could try this:

    window.onresize = updateBodySize;

    0 讨论(0)
  • 2021-01-28 08:15
    $( document ).ready(function() {
      $(window).resize(updateBodySize());
    });
    
    0 讨论(0)
  • 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.

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