简体   繁体   English

$(窗口).resize(); 不起作用

[英]$(window).resize(); doesn't work

I've got a problem with window.resize . 我window.resize有问题。 My code js/jquery is here 我的代码js / jquery在这里

var x = $(window).width();
var y = $(window).height();
var z = $('#card').height();
var a = z + 140; // !!!zmienic gdy zmiana marginesu z gory lub paddingu (100 margin + 20x2 padding)
var b = 1.78 * y;
var c = 1.78 * a;
function updateBodySize(){
    x = $(window).width();
    y = $(window).height();
    z = $('#card').height();
    a = z + 140; // !!!zmienic gdy zmiana marginesu z gory lub paddingu (100 margin + 20x2 padding)
    b = 1.78 * y;
    c = 1.78 * a;
    if (c>b) {
        if (x>c) {
            $('body').css({'background-size': x + 'px auto'});
        }
        else {
            $('body').css({'background-size': 'auto ' + c + 'px'});
        }
        $('body').css({'height': a + 'px'});
    }
    else {
        if (x>b) {
            $('body').css({'background-size': x + 'px auto'});
        }
        else {
            $('body').css({'background-size': 'auto ' + b + 'px'});
        }
        $('body').css({'height': y + 'px'});
    }
}
$(document).ready(updateBodySize()); //kiedy zaladowany
$(window).resize(updateBodySize());  //kiedy zmiana rozmiaru

Comments are in Polish but they aren't important. 评论以波兰语写成,但并不重要。 I want to run function updateBodySize every time browser is resized, but now this function runs only when document is ready (so i know that function works correct) (line last but one) and it looks like the last line is ignored. 我想在每次调整浏览器大小时运行一次updateBodySize函数,但是现在此函数仅在文档准备就绪时运行(所以我知道该函数可以正常运行)(最后一行但只有一行),并且看起来最后一行被忽略了。 Is this line wrong $(window).resize(updateBodySize()); 这行是错误的$(window).resize(updateBodySize()); ? Or is something worng with my function? 还是我的功能不合时宜? I checked this code in Chrome 33. 我在Chrome 33中检查了此代码。

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 . 请注意()updateBodySize -您的目标是将updateBodySize 函数 updateBodySize.ready.resize ,而不是其结果 By call the function instead, what you're doing is passing the result of updateBodySize() to the .ready and .resize functions, in effect: 通过调用该函数,您正在做的就是将updateBodySize()的结果传递给.ready.resize函数,实际上是:

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

Which, as you've noticed, does nothing except what updateBodySize() does first (two) times you called it. 正如您所注意到的,除了updateBodySize()第一次(两次)调用它所做的之外,它什么都不做。 Drop the () and it will be treated as the event handler you expect. 删除() ,它将被视为您期望的事件处理程序。

PS : 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. 在函数块之前,可以将其删除,因为您在函数重新定义了这些var,因此它将在调用时独立地计算它们。

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

Try this: 尝试这个:

$(function(){updateBodySize();}); //kiedy zaladowany
$(window).resize(function(){updateBodySize();}); 
$( document ).ready(function() {
  $(window).resize(updateBodySize());
});

Or you could try this: 或者您可以尝试以下方法:

window.onresize = updateBodySize; window.onresize = updateBodySize;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM