css - 使元素在没有 JS 的情况下占用 100% 的可见空白空间
问题描述
所以......我如何只用 CSS 做这样的事情?观看红色元素,它缩放而不是滚动:
目前它通过 JS 工作,我根据计算的可用空间捕获元素的滚动和更新大小。
这就是它现在的工作方式: https ://codepen.io/bswan-the-decoder/pen/OJPVxZj
<div class="header">
Header
</div>
<div class="header-spacer">
</div>
<div class="content">
<div class="sidebar">
</div>
<div class="page">
</div>
</div>
<div class="footer">
Footer
</div>
body {
background-color: black;
}
.header {
position: fixed;
width: 100%;
top: 0px;
left: 0px;
min-height: 80px;
background-color: #FFF;
z-index: 500;
}
.footer {
min-height: 50px;
bottom: 0px;
background-color: #FFF;
left: 0px;
}
.page {
min-height: 1000px;
width: 50%;
margin: 15px;
margin-left: auto;
margin-right: auto;
background-color: #AAA;
}
.sidebar {
position: fixed;
background-color: #F66;
left: 10px;
top: 105px;
width: 100px;
height: 200px;
}
let header = document.querySelector('.header');
let footer = document.querySelector('.footer');
let sidebar = document.querySelector('.sidebar');
let header_spacer = document.querySelector('.header-spacer');
header_spacer.style.height = header.clientHeight+'px';
document.addEventListener('scroll',(event)=>{
let height = window.innerHeight - header.clientHeight - 35;
let scroll = this.scrollY;
if(scroll+window.innerHeight+footer.clientHeight>=document.body.clientHeight)
{
height = (height - (
(scroll+window.innerHeight+footer.clientHeight)
- document.body.clientHeight));
}
sidebar.style.height = height+'px';
});
这是我的新布局: https ://codepen.io/bswan-the-decoder/pen/wvBapvV
<body>
<div class="header">
</div>
<div class="content">
<div class="sidebar_holder">
<div class="sidebar"></div>
</div>
<div class="page_holder">
<div class="page"></div>
</div>
</div>
<div class="footer">
</div>
</body>
body{
display: grid;
grid-template-rows: min-content auto min-content;
grid-template-columns: auto;
grid-template-areas: "header" "content" "footer";
background-color: #000;
min-height: 100vh;
}
.header{
grid-area: header;
background-color: #FFF;
min-height: 80px;
position: sticky;
top: 0px;
z-index: 5;
}
.footer{
grid-area: footer;
background-color: #FFF;
min-height: 40px;
}
.content{
grid-area: content;
display: grid;
grid-template-rows: auto;
grid-template-columns: min-content auto;
grid-template-areas: "sidebar page";
}
.sidebar_holder {
grid-area: sidebar;
}
.page_holder {
grid-area: page;
}
.sidebar {
width: 100px;
position: sticky;
top: 105px; /*how to make it (header height+20px) ?*/
height: 100px; /*How to make it (100vh - header height - visible height of footer)?*/
background-color: #F66;
}
.page {
height: 1000px;
width: 50%;
background-color: #AAA;
margin: 25px;
margin-left:auto;
margin-right:auto;
}
更新:
@Roddy - 我试过了,但我缺乏知识,所以我没有任何用处。
解决方案
要使用 div 填充整个空间,您可以使用ViewHeight
和ViewWidth
单位,例如:
vw
并vh
取屏幕大小的百分比并应用它,100vw 表示宽度的 100%
.whole-area {
height: 100vh;
width: 100vw;
background: orange;
}
<div class="whole-area">
<span>Text</span>
</div>
推荐阅读
- c# - 为什么 Visual-Studio 会为一个应用程序自动生成 app.config 而不是另一个应用程序?
- c# - C# DrawLines 到 DrawBeziers
- python - Holoviews 曲线中的关键尺寸用于在字典中查找值
- android - 在 Android 的 Webview 中嵌入 Spotify 无法正常工作
- c# - C# 将时间戳添加到 PKCS#7 CMS 数字签名
- python - Python:获取列表的可能性并更改循环数
- laravel - 如何使用 FilePond 上传图片?
- bash - 使用 bash 在 n 斜杠之间捕获文本
- python - TypeError的解决方法是什么:QTreeWidget.setModel() is a private method
- node.js - NodeJS 向 Laravel 服务器发送请求错误