javascript - jquery调整大小后元素高度不更新

标签 javascript jquery html

调整大小时,它不会更新高度。一旦负载设置了它的高度,它就不会更新,尽管我更新了变量。

如果我删除设置高度的行,那么我的变量更新正常,但是一旦设置了高度,它就什么都不做。

我哪里做错了?

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

    });
});

这是一个 JS fiddle
https://jsfiddle.net/5c1za6xa/

最佳答案

在窗口调整大小函数中包含 var hero_height = $('.hero-image').outerHeight();

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

关于javascript - jquery调整大小后元素高度不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38977498/

相关文章:

javascript - 仅使用 JavaScript 捕获 ESC 键

javascript - 如何将关联数组从 PHP 传递到 HTML 以进行进一步处理

html - 单独使用 CSS,如何制作一个动态大小的 4 :3 aspect ratio div also stay centered?

javascript - 如何解决Uncaught ReferenceError : bus is not defined? (vue.js 2)

Javascript 函数 - 将字符串参数转换为运算符

javascript - 在 .net MVC 中使用 jquery 变量作为 ActionLink 的参数

jquery - Phonegap/cordova IOS 横向视频

jquery - 在 jQuery Draggable 拖动事件期间获取放置目标

javascript - 加载另一个页面后从登陆页面重定向

javascript - 加载资源失败:net::ERR_EMPTY_RESPONSE http://test.com