jQuery $(window).resize Chrome not updating

jQuery $(window).resize Chrome not updating

本文关键字:Chrome not updating resize window jQuery      更新时间:2023-09-26

我正在使用jquery来获取高度,但是当有人调整浏览器大小时,这种情况会发生变化。我正在使用以下代码:

$(function() {
    var wheight = $('.home').height(); // Get height of browser window
    var wWidth = $(window).width(); // Get width of browser window
    if ( wWidth >= 992 ) {
        $('.home').css('height', wheight);
    }
})
$(window).resize(function() {
    var wheight = $('.home').height(); // Change the height of browser resize
    var wWidth = $(window).width(); // Change the width of browser resize
});

它在Firefox中工作正常,但是在Chrome中,当您缩小浏览器窗口时它不起作用,但是如果您扩展浏览器窗口,它可以工作。知道我该如何解决这个问题吗?

您正在调整文档就绪的 .home 元素的大小。调整窗口大小时不会。尝试:

var wheight, wWidth;
$(function() {
    wheight = $('.home').height();
    wWidth = $(window).width();
    if ( wWidth >= 992 ) {
        $('.home').css('height', wheight);
    }
});
$(window).resize(function() {
    wheight = $('.home').height(); // Change the height of browser resize
    wWidth = $(window).width(); // Change the width of browser resize
    if ( wWidth >= 992 ) {
        $('.home').css('height', wheight);
    }
});

甚至更好:

var wheight, wWidth;
$(function () {
    resizeHome();
});
$(window).resize(function () {
    resizeHome();
});
function resizeHome() {
    wheight = $('.home').height(); // Change the height of browser resize
    wWidth = $(window).width(); // Change the width of browser resize
    if (wWidth >= 992) {
        $('.home').css('height', wheight);
    }
}