首页 > web前端 > js教程 > 正文

创建可滚动的覆盖层,位于固定头部和底部之间

霞舞
发布: 2025-11-01 17:01:00
原创
965人浏览过

创建可滚动的覆盖层,位于固定头部和底部之间

本文旨在解决如何使用纯CSS创建一个可滚动的覆盖层,该覆盖层位于页面固定头部和动态高度的底部之间,且不与头部和底部重叠。我们将利用`calc()`函数和相对定位,根据视口高度、头部高度和底部高度动态计算覆盖层的最大高度,实现预期的布局效果。

实现原理

核心思想是利用CSS的calc()函数动态计算覆盖层的最大高度。具体来说,覆盖层的最大高度等于视口高度减去头部高度和底部高度。为了确保覆盖层位于底部之内,我们将使用position: relative和bottom: calc(100%)将覆盖层相对于底部定位。

具体步骤

  1. 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。

  2. CSS样式:

    美间AI
    美间AI

    美间AI:让设计更简单

    美间AI45
    查看详情 美间AI
    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的样式:

    • position: absolute;:使其可以相对于父元素(.footer-wrapper)进行定位。
    • bottom: calc(100%);:将覆盖层的底部与底部的顶部对齐,利用了底部的高度作为计算的基准。
    • max-height:calc(100vh - 100% - 58px);:计算覆盖层的最大高度,100vh是视口高度,100%是底部的高度,58px是头部的高度(包含margin-top)。
    • overflow-y: auto;:允许内容滚动。
    • width: 100%;: 确保宽度与footer一致。 .footer-wrapper的position: relative;是使#footer-content可以相对于它进行绝对定位的关键。
  3. 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>
登录后复制

注意事项

  • 确保引入jQuery库,以便使用JavaScript代码。
  • 头部高度和底部高度的计算需要根据实际情况进行调整。
  • 如果底部高度是动态变化的,则需要考虑使用JavaScript来动态更新覆盖层的最大高度。

总结

通过结合CSS的calc()函数和相对定位,我们可以创建一个可滚动且位于固定头部和动态底部之间的覆盖层,而无需使用JavaScript来动态计算高度。这种方法简单有效,适用于各种需要类似布局的场景。

以上就是创建可滚动的覆盖层,位于固定头部和底部之间的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号