$(window).resize();不起作用 [英] $(window).resize(); doesn't work

查看:724
本文介绍了$(window).resize();不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

window.resize有问题.我的代码js/jquery在这里

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 = 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

评论用波兰语写,但并不重要.我想在每次调整浏览器大小时运行一次updateBodySize函数,但是现在此函数仅在文档准备就绪时运行(所以我知道该函数可以正常运行)(最后一行但只有一行),并且看起来最后一行被忽略了.这行是错误的$(window).resize(updateBodySize());吗?还是我的功能不合时宜?我在Chrome 33中检查了此代码.

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. Is this line wrong $(window).resize(updateBodySize());? Or is something worng with my function? I checked this code in Chrome 33.

推荐答案

您的最后两行似乎是这里的问题:

Your final two lines seem to be the problem here:

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

应该是:

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

请注意,将()updateBodySize删除-您的目标是将功能 updateBodySize传递给.ready.resize,而不是其结果.通过调用该函数,实际上是将updateBodySize()的结果传递给.ready.resize函数,

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

正如您所注意到的,

除了updateBodySize()第一次(两次)调用它所做的以外,什么都不做.删除(),它将被视为您期望的事件处理程序.

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 :

除非您使用的是第一段

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;

在功能块之前,您可以将其删除,因为您在功能内重新定义了这些var ,因此它将在调用时独立地计算它们.

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.

这篇关于$(window).resize();不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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