【问题标题】:update height of element after a resize调整大小后更新元素的高度
【发布时间】:2016-03-19 08:19:37
【问题描述】:

如何在调整大小事件后知道元素的高度?我使用 jQuery 的函数 .height() 并且在使用我的调整大小函数之前我有一个 500 毫秒的计时器,但是函数 .height() 仍然返回调整大小之前的高度。

$(window).resize(function() {
    window.clearTimeout(timerResize);
    timerResize = setTimeout(recalculateAll, 1000);
});

function recalculateAll() {
    console.log('resize');
    setOpenClose('.auteurs .block');
}

function setOpenClose(target) {
    $(target).each(function() {
        $(this).data('height', $(this).height());
        $(this).height($(this).height() - ($(this).find('p').height() + 45));
    });
    sameHeights(target);
}

function sameHeights(target, locus) {
    if (locus === undefined) {
        locus = target;
    }
    var heightMax = 1;
    $(target).each(function() {
        if ($(this).height() > heightMax) {
            heightMax = $(this).height();
        }
    });
    $(target).each(function() {
        $(this).height(heightMax);
    });
}

希望我的问题是可以理解的,因为我的英语很接近,而且我是开发新手。

【问题讨论】:

    标签: javascript jquery resize height


    【解决方案1】:

    您应该能够在调整大小回调中调用检索元素的高度。

    $(window).resize(function(){
        var itemHeight = $('.item').height();
    });
    

    见https://jsfiddle.net/cvwf0pra/

    【讨论】:

    • 它很实用,所以谢谢,如果找不到更好的我会使用它,但我不喜欢为此在回调中重写我的函数,我会看到回调属性找到一种传播方式财产的新价值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 2012-12-22
    相关资源
    最近更新 更多