简体   繁体   English

如何仅将垂直滚动条添加到一个div中而不指定高度?

[英]How to add vertical scroll bar to one div only without specifying height?

Can I add a scroll bar to a div when needed but without specifying any height either for the div itself or for a container that this div must be in? 是否可以在需要时向div添加滚动条,但不为div本身或该div必须位于的容器指定任何高度

The div is the very botton element on the page and I want user to be able to resize the browser window. div是页面上非常botton的元素,我希望用户能够调整浏览器窗口的大小。 So the scroll bar would appear when the "data" that dynamically add to the div reaches the botton of the page. 因此,当动态添加到div的“数据”到达页面的底部时,滚动条就会出现。

My page got contains only one table and second row of the table is below code. 我的页面仅包含一个表,该表的第二行在代码下方。

<table border="0">
 <tr>
    <td><button type="button" onclick="$('#info').text('')" > clear info area </button></td>
 </tr>
 <tr class="tr_info">
    <td><div id='info'></div></td>  
 </tr>
</table>

I tried overflow-y: scroll; 我尝试了overflow-y: scroll; to the div but it added scroll bar to the whole window not the div. 到div,但它向整个窗口(而不是div)添加了滚动条。

html, body { height: 100%; }
div { height: 100%; overflow: auto; }

http://jsfiddle.net/Wexcode/xMdPA/ http://jsfiddle.net/Wexcode/xMdPA/

Less text: http://jsfiddle.net/Wexcode/xMdPA/1/ 较少的文字: http //jsfiddle.net/Wexcode/xMdPA/1/

You can also get the text to stay at the bottom of the page: http://jsfiddle.net/Wexcode/xMdPA/2/ 您还可以将文本保留在页面底部: http : //jsfiddle.net/Wexcode/xMdPA/2/

Fiddle http://jsfiddle.net/RSh9H/2/show/ 小提琴http://jsfiddle.net/RSh9H/2/show/

Corrected Fiddle : http://jsfiddle.net/RSh9H/8/show/ 更正的小提琴: http : //jsfiddle.net/RSh9H/8/show/

jQuery: jQuery的:

$(window).on('load resize',function(){
var heightOfWindow = $(window).height();
var totalHeightOfContents = $(document).height();
    if(heightOfWindow <= totalHeightOfContents){
        var heightExcludingInfo = $('table#main').height() - $('#info').height();
        var availableHeightForInfo = heightOfWindow - heightExcludingInfo;
        $('#info').height(availableHeightForInfo);
    }
    else{
        $('#info').css({height:'auto'});
    }
});

going off @zenkaty 's post, @zenkaty的帖子,

in the style for that element, put overflow-y:auto , this way when it has an overflow it will show a scroller, but if not it will have no scrollbar. 在该元素的样式中,放入overflow-y:auto ,这样,当它发生溢出时,将显示一个滚动条,但如果没有,则没有滚动条。

(as opposed to overflow-y:scroll which will always show a scroll bar. (与总是显示滚动条的overflow-y:scroll相反。

Edit: 编辑:

As Sabithpocker pointed out, if you have no height, as far as I know, the table will expand to show all text if there is too much. 正如Sabithpocker所指出的那样,据我所知,如果您没有身高,那么表格将扩展为显示所有文本,如果过多的话。 If you set a height, then you can use overflow-y:auto . 如果设置高度, 可以使用overflow-y:auto Please see http://jsfiddle.net/rZV5u/ for example. 例如,请参阅http://jsfiddle.net/rZV5u/

Hope this helps! 希望这可以帮助!

Just put this on your div - if you add it to the body/html, then the whole window will scroll, obviously. 只需将其放在div上-如果将其添加到body / html中,显然整个窗口将滚动。

div {
  overflow-y: scroll;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM