Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Element height not updating after jquery resize

On resize it doesn't update the height. Once the load has set it's height, it will then not update despite me updating the variable.

If I take out the lines where it sets the height, then my variable updates fine, but once the height is set it just doesn't do anything.

Where have I gone wrong?

var hero_height = $('.hero-image').outerHeight();
console.log('heroHeight: ' + hero_height);

$(document).ready(function() {
    $(window).load(function() {

        $('.hero-image').css('height', hero_height );

    });

    $(window).resize(function() {

        if (hero_height !== $('.hero-image').outerHeight()) {
            $('.hero-image').css('height', hero_height );
        };

        hero_height = $('.hero-image').outerHeight();
        console.log('heroHeight: ' + hero_height);

    });
});

Here is a JS fiddle
https://jsfiddle.net/5c1za6xa/

like image 871
KINKWILDE Avatar asked Aug 16 '16 14:08

KINKWILDE


1 Answers

Include var hero_height = $('.hero-image').outerHeight(); in the window resize function.

$(window).resize(function() {
    var hero_height = $('.hero-image').outerHeight();
    if (hero_height !== $('.hero-image').outerHeight()) {
        $('.hero-image').css('height', hero_height );
    };

    hero_height = $('.hero-image').outerHeight();
    console.log('heroHeight: ' + hero_height);
});
like image 116
Ghayyour Ahmed Butt Avatar answered Oct 02 '22 07:10

Ghayyour Ahmed Butt