admin管理员组文章数量:1317906
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?
The div is the very botton element on the page and I want user to be able to resize the browser window. So the scroll bar would appear when the "data" that dynamically add to the div reaches the botton of the page.
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;
to the div but it added scroll bar to the whole window not the div.
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?
The div is the very botton element on the page and I want user to be able to resize the browser window. So the scroll bar would appear when the "data" that dynamically add to the div reaches the botton of the page.
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;
to the div but it added scroll bar to the whole window not the div.
- Your page has only this table? – sabithpocker Commented Aug 29, 2012 at 23:27
- 1 I have added javascript and jquery tags assuming that there can be no pure css solution. – sabithpocker Commented Aug 29, 2012 at 23:36
- @sabithpocker: basically yes :-) but the code I posted is in the second row of the main table. – Radek Commented Aug 29, 2012 at 23:36
- I assume what you want is to share available window height between elements so that everything fits in, if there is not enough space put a sroll bar on "tr_info". Is it what you want? – sabithpocker Commented Aug 29, 2012 at 23:43
4 Answers
Reset to default 2Fiddle http://jsfiddle/RSh9H/2/show/
Corrected Fiddle : http://jsfiddle/RSh9H/8/show/
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'});
}
});
html, body { height: 100%; }
div { height: 100%; overflow: auto; }
http://jsfiddle/Wexcode/xMdPA/
Less text: http://jsfiddle/Wexcode/xMdPA/1/
You can also get the text to stay at the bottom of the page: http://jsfiddle/Wexcode/xMdPA/2/
going off @zenkaty 's post,
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.
(as opposed to overflow-y:scroll
which will always show a scroll bar.
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. If you set a height, then you can use overflow-y:auto
. Please see http://jsfiddle/rZV5u/ for example.
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 {
overflow-y: scroll;
}
本文标签: javascriptHow to add vertical scroll bar to one div only without specifying heightStack Overflow
版权声明:本文标题:javascript - How to add vertical scroll bar to one div only without specifying height? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742031221a2416478.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论