基于窗口大小/调整大小的div的动态最大高度

时间:2015-09-02 14:23:58

标签: jquery css html5 css3

Coding / jQuery初学者。

我试图找出如何根据屏幕尺寸调整div的最大高度,div位于顶部'.top-nav'和底部'.footer' div之间。基本上,当页面加载或调整窗口大小时,不会出现滚动条。

我需要调整大小的div '.color-container',包含一系列在悬停时变为随机颜色的列表。如果我有足够的列表元素,我认为'.color-container'的max-height属性会在适当的位置切断它们。

根据我的研究,我需要调用.resize函数,但我似乎无法使其工作。

我的jQuery代码:

function resetHeight(){
    var newHeight = $(window).height() - $('.top-nav').outerHeight() - $('.footer').outerHeight();
    $('.color-container').css('max-height', newHeight);
}

$(function(){
    newHeight();

    $(window).resize(function(){
      resetHeight();
    });
});

JSFiddle:http://jsfiddle.net/tgdude/2qsq725e/14/

1 个答案:

答案 0 :(得分:0)

这不是问题的确切答案,但我在课程中使用了position: absolute;,希望对您有所帮助,请查看http://jsfiddle.net/2qsq725e/19/