CSS 如何为特定 div 启用滚动并禁用页面滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18411215/
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-08-30 00:04:34  来源:igfitidea点击:

How to Enable scroll for specific div and disable scroll for page

cssscroll

提问by Caspert

Is it possible to disable scroll for the page, and only enable for a specific divinside the page?

是否可以禁用页面滚动,而只启用div页面内的特定内容?

回答by Tatarin

to disable it use:

禁用它使用:

css

css

.disableScroll{
      overflow-y:hidden;
      overflow-x:hidden;
}

just add <body class="disableScroll">to the disable

只需添加<body class="disableScroll">到禁用

inline style<body style="overflow:hidden">

内联样式<body style="overflow:hidden">

for div use, leave scrolling enabled <div style="overflow:auto;">

对于 div 使用,启用滚动 <div style="overflow:auto;">

Keep in mind that you can use overflow-y for vertical scroll and overflow-x for horizontal scroll properties

请记住,您可以将溢出-y 用于垂直滚动,将溢出-x 用于水平滚动属性

回答by patrick

It's ok a css only solution?

仅 css 的解决方案可以吗?

If so, just add overflow: hidden to body and overflow: auto to the scrollable div.

如果是这样,只需将 overflow: hidden 添加到 body 和 overflow: auto 到可滚动的 div。

body {
  overflow: hidden;
}

#scroller {
  overflow: auto;
  height: 100px;
}

Example

例子

回答by patrick

for whole page:

对于整个页面:

body {
  height: 100%;
  width: 100%;
}

for div for example:

以 div 为例:

div.scroll {
    width:180px;
    height:170px;
    overflow:auto;

}

in html:

在 html 中:

<div class='scroll'></div>

回答by Joe Mike

HTML:

HTML:

<div class="scrollDisable"></div>

CSS:

CSS:

.scrollDisable{
  overflow-y:hidden;
  overflow-x:hidden;

}

}

For whole page :

对于整个页面:

 body {
  height: 100%;
  width: 100%;
}

Hope this will be helpful.

希望这会有所帮助。