检测到浏览器调整大小不起作用

时间:2013-05-05 08:10:29

标签: jquery

我试图在浏览器调整大小时使jQuery警报,但它不起作用。

http://jsfiddle.net/5m6yG/

function browser_resize(){
    var window_height = $(window).height();

    if ((window_height>=800)) {
        alert('800 or above!');
    }
    if ((window_height<800)) {
        alert('Under 800!');
    }
}
$(window).resize(browser_resize);

这不起作用。当我调整浏览器的大小,甚至超过800时,它仍然警告“800以下!”。有什么问题?

3 个答案:

答案 0 :(得分:3)

这是因为$(window).height();为您提供了result iframe的高度,而不是浏览器的高度。

请参阅jsFiddle

function browser_resize(){
    $("#height").html($(window).height());
}
$(window).resize(browser_resize);

Another demo with 300px height

function browser_resize(){
    var window_height = $(window).height();

    if ((window_height>=300)) {
        $("#height").html('300 or above!');
    }
    if ((window_height<300)) {
        $("#height").html('Under 300!');
    }
}
browser_resize();
$(window).resize(browser_resize);

答案 1 :(得分:1)

为了进行测试,我的高度为200,这对我来说很好:

function browser_resize() {
    var window_height = $(window).height();
    console.log(window_height)
    if (window_height >= 200) {
        console.log('200 or above!');
    } else {
        console.log('Under 200!');
    }
}
$(window).resize(browser_resize);

您需要检查浏览器控制台。

另外,我认为这里不需要第二个 if 语句。您可以将其放在 else 部分中。

答案 2 :(得分:1)

检查这个sout 演示http://jsfiddle.net/5m6yG/4/

的jQuery

function browser_resize(){
    var window_height = $(window).height();

    if ((window_height>=500)) {
        alert('500 or above!');
    }
    else if ((window_height<500)) {
        alert('Under 500!');
    }
}
$(window).resize(browser_resize);