'div' 的滚动条管理使用 JavaScript 或 jquery
有没有办法在 javascript 或 jquery 中同时根据浏览器大小和“div”大小来控制滚动条的寿命。例如,如果“div”内容溢出,则向 div 添加滚动条,如果浏览器将大小调整到更高的高度,则从 div 中删除滚动条,因为它将随浏览器一起拉伸:
$("#wrapper").height($(window).height() - 215);
var elem = document.getElementById("topPanel");
var errHolder = document.getElementById("error");
if(elem.scrollHeight > elem.clientHeight - errHolder.scrollHeight){
$(elem).css({
"overflow-y": "auto",
"overflow-x": "hidden",
height: elem.scrollHeight - errHolder.scrollHeight
});
}
$(window).resize(function(e){
if(this.clientHeight > 200){
$(elem).css({
"overflow-y": "auto",
"overflow-x": "hidden",
height: "200px"
});
}
else {
$(elem).css({
"overflow-y": "auto",
"overflow-x": "hidden",
height: elem.scrollHeight - errHolder.scrollHeight
});
}
});
CSS:
#toolPanel {
position: relative;
width: 100%;
height: 100%;
}
#topPanel {
overflow: auto;
height: 315px;
width: 100%;
background: #069;
border: 1px solid #0CC;
}
#bottomPanel {
position: absolute;
background: #0CC;
border: 1px solid #069;
height: 65px;
width: 100%;
bottom: 0;
}
HTML:
<div id="wrapper">
<div id="toolPanel">
<div id="error"></div>
<div id="topPanel">
<!-- Content that may overflow -->
</div>
<div id="bottomPanel"> </div>
</div>
</div>
This is just a head -开始,但我迷路了...我不擅长使用 clientHeight/scroll、offset 等。请帮忙。
Is there a way to control a scrollbar's life depending on the browser size and the 'div' size at the same time in javascript or jquery. For example, if the 'div' content is overflowing then add scrollbar to div and if the browser is resized to a higher height then remove scrollbar from div, since it's gonna be stretched with the browser:
$("#wrapper").height($(window).height() - 215);
var elem = document.getElementById("topPanel");
var errHolder = document.getElementById("error");
if(elem.scrollHeight > elem.clientHeight - errHolder.scrollHeight){
$(elem).css({
"overflow-y": "auto",
"overflow-x": "hidden",
height: elem.scrollHeight - errHolder.scrollHeight
});
}
$(window).resize(function(e){
if(this.clientHeight > 200){
$(elem).css({
"overflow-y": "auto",
"overflow-x": "hidden",
height: "200px"
});
}
else {
$(elem).css({
"overflow-y": "auto",
"overflow-x": "hidden",
height: elem.scrollHeight - errHolder.scrollHeight
});
}
});
CSS:
#toolPanel {
position: relative;
width: 100%;
height: 100%;
}
#topPanel {
overflow: auto;
height: 315px;
width: 100%;
background: #069;
border: 1px solid #0CC;
}
#bottomPanel {
position: absolute;
background: #0CC;
border: 1px solid #069;
height: 65px;
width: 100%;
bottom: 0;
}
HTML:
<div id="wrapper">
<div id="toolPanel">
<div id="error"></div>
<div id="topPanel">
<!-- Content that may overflow -->
</div>
<div id="bottomPanel"> </div>
</div>
</div>
This is just a head-start but I'm lost... I'm not good with the use of clientHeight/scroll, offset, etc. Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以用 css 来做到这一点:
You can do that with css: