1

我怎么能用 jQuery 做到这一点:http: //jsfiddle.net/tdskate/C4nAG/

我需要一个 div 自动将高度调整为剩余多少可用垂直空间。

我试图用它来计算它,$('body > *').height()然后从中减去元素的高度......但是当某些元素具有顶部或底部边距时,它不能正确计算。

此外,当浏览器窗口调整大小时,它需要调整大小......

4

1 回答 1

2

这应该有效:

$(function() {
    $(window).resize(function() {
        var other_element_heights = 0;

        $('body').children().each(function() {
            other_element_heights += $(this).outerHeight(true);
        });

        other_element_heights -= $('.auto-height').height();

        $('.auto-height').height($(window).height() - other_element_heights);
    }).resize();   
});

http://jsfiddle.net/C4nAG/82/

于 2011-10-31T21:03:42.367 回答