5

好的,我有一个 div 类“内容”,每当浏览器将高度调整为 800 像素时,内容 div 会将其最大高度调整为 500 像素。我遇到的问题是,如果浏览器以 800px 的高度打开,那么除非我刷新浏览器,否则 div 没有滚动条。

我的代码:

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

此外,我正在使用 jScrollPane,如果我在 css 中设置最大高度,它工作正常,但如果我使用上面的代码,而不是使用常规滚动条。任何帮助将不胜感激。

4

3 回答 3

12

试试这个:

$(document).ready(function() {

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

})
于 2012-08-16T21:28:57.313 回答
0

调用该代码一次$(document).ready()

于 2012-08-16T21:29:09.157 回答
0

把它包起来

$(function() {
    //Script in here should run on document.ready
});
于 2012-08-16T21:29:29.560 回答