如何将可滚动主体放入视口中?
此表中有一个页眉和一个页脚,其中有行。它们的高度取决于内部组件。但我希望主体能够容纳其余空间,以便占据视口的整个高度。
const body = document.getElementById('body');
const createFakeData = (id) => {
const row = document.createElement('div')
row.innerHTML = "Row" + id;
row.classList.add('row')
body.appendChild(row);
}
for (let i = 0; i < 100; i++) {
createFakeData(i);
}
.row {
background-color: red;
padding: 10px;
margin: 0 0 5px 0;
}
.container {
height: 100vh;
}
.header {
height: 100px; /*Random value, height depends on components inside*/
background-color: yellow;
}
.body {
overflow-y: scroll;
/*height: something; Height should take the remaining space so everything fits view port*/
}
.footer {
height: 100px; /*Random value, height depends on components inside*/
background-color: orange;
}
<div class="container">
<div class="header">Header</div>
<div class="body" id="body"></div>
<div class="footer">Footer</div>
</div>
只需用这个替换你的 body 类的 css :
.body {
overflow-y: scroll;
height: calc(100vh - 200px);
}