0

0

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

霞舞

霞舞

发布时间:2025-11-01 17:01:00

|

1005人浏览过

|

来源于php中文网

原创

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

本文旨在解决如何使用纯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样式:

    Vondy
    Vondy

    下一代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来动态计算高度。这种方法简单有效,适用于各种需要类似布局的场景。

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

WorkBuddy
WorkBuddy

腾讯云推出的AI原生桌面智能体工作台

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
jquery插件有哪些
jquery插件有哪些

jquery插件有jQuery UI、jQuery Validate、jQuery DataTables、jQuery Slick、jQuery LazyLoad、jQuery Countdown、jQuery Lightbox、jQuery FullCalendar、jQuery Chosen和jQuery EasyUI等。本专题为大家提供jquery插件相关的文章、下载、课程内容,供大家免费下载体验。

156

2023.09.12

jquery怎么操作json
jquery怎么操作json

操作的方法有:1、“$.parseJSON(jsonString)”2、“$.getJSON(url, data, success)”;3、“$.each(obj, callback)”;4、“$.ajax()”。更多jquery怎么操作json的详细内容,可以访问本专题下面的文章。

337

2023.10.13

jquery删除元素的方法
jquery删除元素的方法

jquery可以通过.remove() 方法、 .detach() 方法、.empty() 方法、.unwrap() 方法、.replaceWith() 方法、.html('') 方法和.hide() 方法来删除元素。更多关于jquery相关的问题,详情请看本专题下面的文章。php中文网欢迎大家前来学习。

406

2023.11.10

jQuery hover()方法的使用
jQuery hover()方法的使用

hover()是jQuery中一个常用的方法,它用于绑定两个事件处理函数,这两个函数将在鼠标指针进入和离开匹配的元素时执行。想了解更多hover()的相关内容,可以阅读本专题下面的文章。

515

2023.12.04

jquery实现分页方法
jquery实现分页方法

在jQuery中实现分页可以使用插件或者自定义实现。想了解更多jquery分页的相关内容,可以阅读本专题下面的文章。

312

2023.12.06

jquery中隐藏元素是什么
jquery中隐藏元素是什么

jquery中隐藏元素是非常重要的一个概念,在使用jquery隐藏元素之前,需要先了解css样式中关于元素隐藏的属性,比如display、visibility、opacity等属性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

128

2024.02.23

jquery中什么是高亮显示
jquery中什么是高亮显示

jquery中高亮显示是指对页面搜索关键词时进行高亮显示,其实现办法:1、先获取要高亮显示的行,获取搜索的内容,再遍历整行内容,最后添加高亮颜色;2、使用“jquery highlight”高亮插件。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

183

2024.02.23

jQuery 正则表达式相关教程
jQuery 正则表达式相关教程

本专题整合了jQuery正则表达式相关教程大全,阅读专题下面的文章了解更多详细内容。

51

2026.01.13

TypeScript类型系统进阶与大型前端项目实践
TypeScript类型系统进阶与大型前端项目实践

本专题围绕 TypeScript 在大型前端项目中的应用展开,深入讲解类型系统设计与工程化开发方法。内容包括泛型与高级类型、类型推断机制、声明文件编写、模块化结构设计以及代码规范管理。通过真实项目案例分析,帮助开发者构建类型安全、结构清晰、易维护的前端工程体系,提高团队协作效率与代码质量。

26

2026.03.13

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Sass 教程
Sass 教程

共14课时 | 0.9万人学习

Bootstrap 5教程
Bootstrap 5教程

共46课时 | 3.6万人学习

CSS教程
CSS教程

共754课时 | 43万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

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