我需要内部元素上的滚动条,而不是浏览器窗口(视口) [英] I need scrollbar on inner element, not browser window (viewport)
问题描述
在下面的代码中,如何使article
容器自动增长以消耗其下面的剩余垂直空间,但滚动条仅保留用于该元素.
In the code below, how do I make the article
container auto grow to consume the remaining vertical space below it, but the scroll bar to remain only for that element.
换句话说,我只希望滚动文章内部而不是整个浏览器窗口.
In other words, I want only the inside of article to scroll and not the entire browser window.
是否有纯CSS解决方案?还是我需要JavaScript来检测浏览器窗口的大小并动态调整文章的height属性?
Is there a pure css solution? Or do I need javascript to detect the size of the browser window and adjust the height property of article dynamically?
html, body {
//height: 100%;
}
#outer_container {
display: flex;
flex-direction: row;
}
#outer2 {
flex: 1 0 auto;
}
#outer2 {
flex: 1 0 auto;
}
#container {
display: flex;
flex-direction: column;
height: 100%;
width: 50%;
background-color: red;
}
#container header {
background-color: gray;
}
#container article {
flex: 1 1 auto;
overflow-y: auto;
height: 0px;
}
#container footer {
background-color: gray;
}
<div id="outer_container">
<div id="outer1">
<h2>Outer 1</h2>
</div>
<section id="container">
<header id="header">This is a header</header>
<article id="content">
This is the content that
<br />With a lot of lines.
<br />With a lot of lines.
<br />This is the content that
<br />With a lot of lines.
<br />
<br />This is the content that
<br />With a lot of lines.
<br />
<br />This is the content that
<br />With a lot of lines.
<br />
</article>
<footer id="footer">This is a footer</footer>
</section>
<div id="outer2">
<h2>Outer 2</h2>
</div>
</div>
http://jsfiddle.net/ch7n6/907/
它最初是基于以下问题的答案:
Flexbox和垂直滚动使用NEWER flexbox api的全高应用程序
It was originally based on the answer to this question:
Flexbox and vertical scroll in a full-height app using NEWER flexbox api
推荐答案
You can try setting position:fixed
to your outer container (http://jsfiddle.net/ch7n6/909/):
#outer_container{
display:flex;
flex-direction:row;
top:0;
bottom:0;
position:fixed;
}
如果它不适用于您的设计,则可以使用window.onresize
事件更改容器尺寸.
If it doesn't work for your design, you can change the container dimensions using window.onresize
event.
这篇关于我需要内部元素上的滚动条,而不是浏览器窗口(视口)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!