'div' 的滚动条管理使用 JavaScript 或 jquery

发布于 2024-09-27 01:59:12 字数 1729 浏览 5 评论 0原文

有没有办法在 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">&nbsp;</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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

裂开嘴轻声笑有多痛 2024-10-04 01:59:12

你可以用 css 来做到这一点:

overflow-y: auto;

You can do that with css:

overflow-y: auto;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文