当前位置: 首页 > backend >正文

滚动条开始滚动时,左侧导航固定,当左侧内容触底到footer时左侧内容取消固定并跟随滚动条滚动

页面上当滚动条滚动时,需要左侧内容固定不动,但是当内容触底到footer时,我们不希望左侧内容压在footer上,视觉上不好看,这时候需要左侧内容取消固定,滚动条在这个范围滚动时,需要左侧内容一直出现在footer上方,直到滚动条滚动超出footer范围,左侧导航继续固定不动。

计算方法:

第一步:计算左侧导航高度,给左侧导航设置fixed属性

第二步:根据(文档的总高度 - 文档内部可见区域的高度 - 元素顶部不可见部分的高度),获取到的高度如果大于footer的总高度,就去除fixed属性,设置absolute属性

        <div class="header bg">header</div><div class="wrapper"><div class="left-box"><div id="left-id" class="bgleft scrollfixed">左侧内容</div></div><div class="content-box bg">content主要内容</div></div><div class="page-footer bg">footer</div>
       .header{height: 100px;margin-bottom: 20px;}.wrapper{display: flex;}.bg{background-color: #2d343c;padding: 20px;border-radius: 5px;color: #fff;}.bgleft{background-color: #29ad9b;padding: 20px;border-radius: 5px;color: #fff;}.left-box{width: 240px;position: relative;}.scrollfixed{position: fixed;}#left-id{width: 200px;height: 600px;}.content-box{height: 2000px;margin: 0 20px;flex: 1;}.right-box{width: 200px;height: 300px;}.page-footer{margin: 20px 0;height: 400px;}.scrollabsolute{position: absolute;bottom: 0;}
window.addEventListener('scroll', function(){const targetTop = $('#left-id').offset().top;const targetBottom = targetTop + $('#left-id').height();const containerTop = $('.page-footer').offset().top;const containerBottom = containerTop - $('.page-footer').height();if (targetBottom - containerTop > 0) {$('#left-id').addClass("scrollabsolute");} const element = document.documentElement; // 获取html文档const scrollHeight = element.scrollHeight; // 文档的总高度const clientHeight = element.clientHeight; // 文档内部可见区域的高度const scrollTop = element.scrollTop; // 元素顶部不可见部分的高度// 计算剩余滚动条距离const remainingScroll = scrollHeight - clientHeight - scrollTop;if(remainingScroll > 440){$('#left-id').removeClass("scrollabsolute")}});

http://www.xdnf.cn/news/17440.html

相关文章:

  • 【Python 小脚本·大用途 · 第 2 篇】
  • 女子试穿4条裤子留下血渍赔50元引争议:消费责任边界在哪?
  • 【星闪】Hi2821 | UART通用异步收发器 + 串口中断收发例程
  • 【人工智能99问】BERT的原理什么?(23/99)
  • 开启单片机
  • 编程基础之多维数组——矩阵交换行
  • 【YOLOv8改进 - C2f融合】C2f融合Outlook Attention:将细粒度特征编码到 token 中, 提高在图像分类和语义分割等任务中的性能
  • 【算法题】:斐波那契数列
  • 【Python】常用内置模块
  • 安全运维工具链全解析
  • Android快速视频解码抽帧FFmpegMediaMetadataRetriever,Kotlin(2)
  • 大模型开发工具的汇总
  • SQL Server从入门到项目实践(超值版)读书笔记 23
  • cursor, vscode黄色波浪线警告问题
  • 从零到精通:嵌入式BLE开发实战指南
  • 计算机网络:(十四)传输层(下)详细讲解TCP报文段的首部格式,TCP 可靠传输的实现与TCP 的流量控制
  • Mybatis和MybatisPlus的对比区分理解法
  • 基于 RabbitMQ 死信队列+TTL 实现延迟消息+延迟插件基本使用
  • 给AI装上“翻译聚光灯”:注意力机制的机器翻译革命
  • Docker 镜像常见标签(如 `标准`、`slim`、`alpine` 和 `noble`)详细对比
  • 编程基础之字符串——统计数字字符个数
  • TypeScript 中的as const是什么?
  • React:useEffect 与副作用
  • token危机解决?扩散模型数据潜力3倍于自回归,重训480次性能仍攀升
  • 浏览器CEFSharp88+X86+win7 之多页面展示(四)
  • LLaMA-Adapter Efficient Fine-tuning of Language Models with Zero-init Attention
  • Redis - 使用 Redis HyperLogLog 进行高效基数统计
  • Spring Boot与WebSocket构建物联网实时通信系统
  • 基于Spring Boot和WebSocket的实时聊天系统
  • go语言运算符