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

横向滚动上方列表查看进度条变化

<!DOCTYPEhtml><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, initial-scale=1.0"><title>横向滚动列表与进度条</title><style>.container{width:80vw;margin:10px auto;box-sizing:border-box;}.list{width:80vw;height:100px;display:flex;overflow-x:auto;scroll-behavior:smooth;scrollbar-width:none;/* Firefox */-ms-overflow-style:none;/* IE/Edge */}.list::-webkit-scrollbar{display:none;/* Chrome/Safari/Opera */}.item{width:100px;height:100px;background-color:#4CAF50;color:white;display:flex;align-items:center;justify-content:center;font-size:18px;font-weight:bold;flex-shrink:0;border:1px solid #45a049;box-sizing:border-box;}.item:nth-child(even){background-color:#2196F3;}.item:nth-child(3n){background-color:#FF9800;}.progress-container{width:40vw;height:40px;margin:20px auto;position:relative;background-color:#e0e0e0;border-radius:5px;}.progress-box{width:10vw;height:100%;position:absolute;left:0;top:0;background-color:#f44336;border-radius:5px;transition:left0.1s ease-out;}.scroll-indicator{text-align:center;margin-top:10px;color:#666;font-size:14px;}</style></head><body><divclass="container"><divclass="list"id="listContainer"><divclass="item">Item1</div><divclass="item">Item2</div><divclass="item">Item3</div><divclass="item">Item4</div><divclass="item">Item5</div><divclass="item">Item6</div><divclass="item">Item7</div><divclass="item">Item8</div><divclass="item">Item9</div><divclass="item">Item10</div></div><divclass="progress-container"id="progressContainer"><divclass="progress-box"id="progressBox"></div></div><divclass="scroll-indicator">横向滚动上方列表查看进度条变化</div></div><script>document.addEventListener('DOMContentLoaded',function(){constlistContainer=document.getElementById('listContainer');constprogressBox=document.getElementById('progressBox');constprogressContainer=document.getElementById('progressContainer');// 设置进度条容器的宽度constcontainerWidth=progressContainer.offsetWidth;constprogressBarWidth=progressBox.offsetWidth;constmaxScrollLeft=listContainer.scrollWidth-listContainer.offsetWidth;// 总可滚动距离// 监听滚动事件listContainer.addEventListener('scroll',function(){// 计算滚动比例 (0 到 1)constscrollRatio=listContainer.scrollLeft/maxScrollLeft;// 计算进度条应移动的距离// 进度条的最大移动距离是容器宽度减去自身宽度constmaxProgressMove=containerWidth-progressBarWidth;constprogressPosition=scrollRatio*maxProgressMove;// 应用位置progressBox.style.left=`${progressPosition}px`;});// 初始化进度条位置progressBox.style.left='0px';});</script></body></html>

效果

这段 HTML + CSS + JavaScript 代码实现了一个横向滚动列表,并配合一个水平进度条来反映用户滚动的位置。下面从整体结构和核心逻辑两个方面进行解释。


🧩 整体结构说明

1.HTML 结构

  • .list:一个横向可滚动的容器,包含多个.item元素(共10个)。
  • .progress-container:一个固定宽度的灰色背景容器,用于显示进度条。
  • .progress-box:红色小方块,作为“进度指示器”,会随着滚动位置左右移动。

2.CSS 样式重点

  • .list设置了overflow-x: autoscroll-behavior: smooth,使其可以横向平滑滚动,并隐藏滚动条(通过各浏览器兼容写法)。
  • .item使用flex-shrink: 0确保不会被压缩,保持固定宽度。
  • .progress-box使用position: absolute定位在.progress-container内部,通过修改left值来移动。

⚙️ 核心逻辑解析(JavaScript 部分)

1.获取关键 DOM 元素

constlistContainer=document.getElementById('listContainer');constprogressBox=document.getElementById('progressBox');constprogressContainer=document.getElementById('progressContainer');

2.计算基础尺寸

constcontainerWidth=progressContainer.offsetWidth;// 进度条容器宽度(例如 40vw)constprogressBarWidth=progressBox.offsetWidth;// 进度条自身宽度(例如 10vw)constmaxScrollLeft=listContainer.scrollWidth-listContainer.offsetWidth;
  • maxScrollLeft是列表最多能向右滚动的距离(总内容宽度 - 可视区域宽度)。

3.监听滚动事件

listContainer.addEventListener('scroll',function(){constscrollRatio=listContainer.scrollLeft/maxScrollLeft;constmaxProgressMove=containerWidth-progressBarWidth;constprogressPosition=scrollRatio*maxProgressMove;progressBox.style.left=`${progressPosition}px`;});
🔍 关键公式:
  • 滚动比例
    scrollRatio = 当前滚动距离 / 最大可滚动距离
    → 范围是0 ~ 1(未滚动到滚到底)。

  • 进度条最大移动距离
    maxProgressMove = 容器宽度 - 进度条自身宽度
    → 因为进度条不能移出容器右边。

  • 进度条当前位置
    progressPosition = scrollRatio × maxProgressMove
    → 将滚动比例映射到进度条的移动范围。

✅ 这样就实现了:滚动列表时,红色进度条同步平滑移动,反映当前滚动进度

4.初始化位置

progressBox.style.left='0px';

确保页面加载时进度条在最左侧(对应未滚动状态)。


🎯 总结:核心思想

将横向滚动的“相对进度”(0% ~ 100%)线性映射到进度条在容器内的“绝对位置”

这种模式常用于:

  • 指示长内容的阅读/浏览进度
  • UI 中的导航辅助(如故事流、商品轮播)
  • 增强用户对可滚动区域长度的感知

http://www.jsqmd.com/news/105896/

相关文章:

  • Natron开源视频合成软件:专业特效制作的终极解决方案
  • 如何快速部署本地AI模型:Lemonade Server完整使用指南
  • vue3 三级路由无法缓存的终终终终终终极解决方案
  • YOLOv9模型评估实战指南:从入门到精通
  • Oxford-Man Institute’s Realized Library现存资源
  • 测试用例合适的粒度
  • 【稀缺资料】资深架构师亲授:高并发下多模态Agent的Docker存储优化策略
  • 如何快速使用ThingsGateway:物联网设备管理的完整指南
  • 27、Vim自动缩进与关键字补全功能全解析
  • 为什么你的云环境总被警告?AZ-500 Agent访问控制配置避坑指南
  • 从零构建Q#-Python项目,精准定位函数调用链的7种高级技巧
  • 揭秘Docker Buildx构建上下文:5个你必须知道的性能优化技巧
  • VSCode + 量子FPGA协同更新机制曝光:未来硬件开发的隐形战场
  • 如何定制Cirq代码补全?掌握这3个高级技巧提升开发效率
  • 手机秒变电影机:Blackmagic Camera + LUT滤镜包的专业级视频解决方案
  • VMware Unlocker完整指南:在普通PC上零成本运行macOS的终极方案
  • VSCode集成Azure QDK的API文档实践(专家级配置全公开)
  • rclone云存储管理实战:从零搭建跨平台数据同步体系
  • 28、Vim 自动补全与语法高亮全解析
  • AI Agent考试部署频频失败?这3类配置错误你一定遇到过
  • GoCV实战指南:构建高效计算机视觉应用完整教程
  • React Native Vision Camera性能调优:从模糊到专业的画质飞跃
  • 【SRE专家亲授】:Docker MCP 网关监控面板的7大核心组件详解
  • 如何构建企业级数据编排平台:Apache DolphinScheduler分布式架构深度解析
  • Q#与Python代码导航实战(20年架构师亲授):构建可维护量子计算项目的5大原则
  • MCP Azure量子认证实验怎么考?7个核心流程一步到位
  • 从零开始学量子计算,手把手教你用VSCode调试Shor算法
  • YOLOSHOW终极指南:零门槛实现专业级目标检测
  • 量子编程效率提升关键:Cirq版本与补全引擎协同优化的4个黄金法则
  • GSE宏编译器完整指南:从零开始掌握魔兽世界自动化战斗