如何在窗口调整大小时调整div max-height的大小?

时间:2012-08-16 05:28:59

标签: jquery html css

我一直在寻找答案。我已经在这里阅读了一些内容,似乎没有人能够解决我想要做的事情。我希望在窗口大小达到某个点时调整div的最大高度。主菜单位于页面底部,内容从菜单中向上滑动。

我的例子:

<div class="content">
    //some content pics/text etc.
</div>

css:

.content { width: 550px; overflow: auto; }`

现在显然我可以在css中设置最大高度但是我不需要它才能生效,直到屏幕尺寸达到800px的高度。如果我在这里遗漏了一些简单的东西,请告诉我。

我愿意使用jQuery或css规则。

3 个答案:

答案 0 :(得分:27)

$(window).on('resize', function(){
    if($(this).height() <= 800){
        $('.content').css('max-height', '800px'); //set max height
    }else{
        $('.content').css('max-height', ''); //delete attribute
    }
});

答案 1 :(得分:4)

我有一个例子:http://jsfiddle.net/sechou/WwpuJ/

$(window).resize(function () { 
   if($(window).height() <= 800){
       $('div.content').css('max-height', 800);
   }else{
       $('div.content').css('max-height', ''); 
   }  
});

答案 2 :(得分:0)

我知道这是一篇过时的文章,但是我想知道,如果仅CSS解决方案在这里会更好(现在会更好)?

.content {
    // No need for max-height here
}

@media(max-height:800px){
    .content {
        max-height:800px;
    }
}