javascript 在单个 div 上启用滚动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4971610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:31:43  来源:igfitidea点击:

Enable scrolling on a single div?

javascriptjquerycssjquery-uihtml

提问by gdanko

I have a layout that looks like this:

我有一个看起来像这样的布局:

<html>
  <head>
    stuff here
  </head>
  <body>
    <div id="master">
      <div id="toolbar">
        <input type="text" id="foo">
      </div>
      <div id="content">
        a whole bunch of content in here
      </div>
    </div>
  </body>
</html>

'#master' is a container for a jquery UI dialog. I'd like the contents of the '#content' div to be scrollable but for '#toolbar' to NOT be scrollable. Is this doable with jquery UI's dialog?

'#master' 是 jquery UI 对话框的容器。我希望'#content' div 的内容是可滚动的,但'#toolbar' 是不可滚动的。这对 jquery UI 的对话框可行吗?

回答by Josiah Ruddell

Just use css rules:

只需使用 css 规则:

#content { overflow: auto; height: [desired height] }

Where you might need to use jQuery is if the modal has a dynamic height. In that case on openof the modal you could set the inner container height. Something like:

如果模态具有动态高度,您可能需要使用 jQuery。在这种情况下,open您可以设置内部容器高度。就像是:

open: function(){
    var modalHeight = $('#master').height();
    $('#content').height(modalHeight - [footer and title]);
}

回答by Chris Baker

Use CSS to give the #contentdiv a set height and optionally a width, and set the CSS property overflow: autoon that div as well. If the content exceeds the height, you'll get a scrollbar.

使用 CSS 为#contentdiv 设置高度和可选的宽度,并overflow: auto在该 div 上设置 CSS 属性。如果内容超过高度,你会得到一个滚动条。