简体   繁体   中英

jQuery $(window).resize Chrome not updating

I am using jquery to get the height of a but this changes when someone resizes the browser. I am using the following code:

$(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
});

It works fine in Firefox however in Chrome it does not work when you shrink the browser window but it works if you expand the browser window. Any idea how I can fix this?

You are resizing the .home element on document ready. Not when the window is resized. Try:

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

Or even better:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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