
本文旨在解决如何使用纯CSS创建一个可滚动的覆盖层,该覆盖层位于页面固定头部和动态高度的底部之间,且不与头部和底部重叠。我们将利用`calc()`函数和相对定位,根据视口高度、头部高度和底部高度动态计算覆盖层的最大高度,实现预期的布局效果。
核心思想是利用CSS的calc()函数动态计算覆盖层的最大高度。具体来说,覆盖层的最大高度等于视口高度减去头部高度和底部高度。为了确保覆盖层位于底部之内,我们将使用position: relative和bottom: calc(100%)将覆盖层相对于底部定位。
HTML结构:
<div class="wrapper">
<div class="header">Header</div>
<div class="content">
Long middle content.....
</div>
</div>
<div class="footer">
Footer <a href="#" id="button">Click me</a>
<div class="footer-wrapper">
<div id="footer-content">Start of footer content
Long footer content....
</div>
</div>
</div>保持与原HTML结构一致,wrapper包含header, content, footer,footer内部包含footer-wrapper,footer-wrapper内部包含footer-content。
CSS样式:
html, body {
height: 100%;
margin: 0;
}
.wrapper {
height: 100%;
display: flex;
flex-direction: column;
max-height: calc(100vh - 1.5rem);
}
.header, .footer {
padding: 10px;
background: silver;
}
.header {
margin-top: 20px;
}
.content {
overflow-y: auto;
min-height: 2.5rem;
padding: 2.5rem;
flex-grow: 1;
position: relative;
background: pink;
}
#footer-content {
display: none;
background: white;
padding: 10px;
overflow-y: auto;
position: absolute;
bottom: calc(100%); /* 将footer-content的底部放置在footer的顶部 */
max-height:calc(100vh - 100% - 58px); /* 视口高度 - footer高度 - header高度 */
width: 100%; /* 确保宽度与footer一致 */
}
.footer-wrapper {
position: relative; /* 相对于footer定位 */
}
.footer {
position: relative;
}关键在于#footer-content的样式:
JavaScript (jQuery) 保持不变:
$(document).ready(function(){
$('#button').click( function(e) {
e.preventDefault(); // stops link from making page jump to the top
e.stopPropagation(); // when you click the button, it stops the page from seeing it as clicking the body too
$('#footer-content').toggle();
});
$('#footer-content').click( function(e) {
e.stopPropagation(); // when you click within the content area, it stops the page from seeing it as clicking the body too
});
});这段JavaScript代码负责切换覆盖层的显示和隐藏,以及阻止事件冒泡。
<!DOCTYPE html>
<html>
<head>
<style>
body { height: 600px; }
#content { background: salmon; display: none; height: 300px; width: 100%; }
html, body {
height: 100%;
margin: 0;
}
.wrapper {
height: 100%;
display: flex;
flex-direction: column;
max-height: calc(100vh - 1.5rem);
}
.header, .footer {
padding: 10px;
background: silver;
}
.header {
margin-top: 20px;
}
.content {
overflow-y: auto;
min-height: 2.5rem;
padding: 2.5rem;
flex-grow: 1;
position: relative;
background: pink;
}
#footer-content {
display: none;
background: white;
padding: 10px;
overflow-y: auto;
position: absolute;
bottom: calc(100%); /* 将footer-content的底部放置在footer的顶部 */
max-height:calc(100vh - 100% - 58px); /* 视口高度 - footer高度 - header高度 */
width: 100%; /* 确保宽度与footer一致 */
}
.footer-wrapper {
position: relative; /* 相对于footer定位 */
}
.footer {
position: relative;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$('#button').click( function(e) {
e.preventDefault(); // stops link from making page jump to the top
e.stopPropagation(); // when you click the button, it stops the page from seeing it as clicking the body too
$('#footer-content').toggle();
});
$('#footer-content').click( function(e) {
e.stopPropagation(); // when you click within the content area, it stops the page from seeing it as clicking the body too
});
});
</script>
</head>
<body>
<div class="wrapper">
<div class="header">Header</div>
<div class="content">
Long middle content.....
</div>
</div>
<div class="footer">
Footer <a href="#" id="button">Click me</a>
<div class="footer-wrapper">
<div id="footer-content">Start of footer content
Long footer content....
</div>
</div>
</div>
</body>
</html>通过结合CSS的calc()函数和相对定位,我们可以创建一个可滚动且位于固定头部和动态底部之间的覆盖层,而无需使用JavaScript来动态计算高度。这种方法简单有效,适用于各种需要类似布局的场景。
以上就是创建可滚动的覆盖层,位于固定头部和底部之间的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号