100%高度div但如果内容更大则相应调整

时间:2012-07-30 11:34:49

标签: javascript jquery fullscreen

我的网站是一个单页网站,其中5个DIV充当面板(类名为.panel),适合拉伸浏览器窗口的整个高度和宽度。我的下面的代码已经做到这一点,但是,我的内容可以大于.panel /浏览器窗口的高度。目前这个内容被隐藏了。即.panel DIV实际上是窗口的高度,没有别的。如果其中的内容大于浏览器窗口的高度,我需要扩展它。

如何编辑以下代码以添加此功能?也许为这个调整后的DIV添加一个类名,以便我可以相应地设置它的样式?

// In my plugins.js file
function fitElements(){
var height=$(window).height();
var width=$(window).width();

$('.panel').css('height',height);
$('.panel').css('width',width)
};

// At the bottom of my HTML file (to call the function)
jQuery(document).ready(function($) {
    fitElements();
});

// My current code also adjusts on browser resize - so would need this answer to do the same
 $(window).resize(fitElements);

1 个答案:

答案 0 :(得分:1)

更改部分

$('.panel').css('height',height);

if (height > $('.panel').height) {
   $('.panel').css('height',height);
}

用于重新调整大小的函数add

$('.panel').css('height','auto'); 

进入函数的开头