当前位置: 首页 > 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.cnnetsun.cn/news/110157.html

相关文章:

  • Obsidian美化资源高效下载实用攻略
  • 酷狗音乐API开发完整指南:从零构建音乐应用
  • Notally:你的终极开源Android笔记应用完整指南
  • 【稀缺资源曝光】国内首个VSCode量子模拟实战手册流出,速看!
  • XVim终极指南:在Xcode中体验Vim的高效编辑魅力
  • Tkinter Helper:可视化拖拽布局工具,让Python GUI开发效率提升10倍
  • 大火的 ChatBI,是如何实现灵活的自然语言数据分析?
  • 33、文本编辑器nvi与Elvis功能解析
  • 35、文本编辑器Elvis与Vile:特性、功能与操作全解析
  • 36、Vile编辑器:功能、初始化与多窗口编辑全解析
  • Subfinder字幕搜索工具:一键解决多平台字幕下载难题
  • Instinct开源智能编辑模型终极指南:重新定义代码编写体验
  • 揭秘多模态Agent依赖冲突:如何用Docker实现高效环境隔离
  • 多版本Agent服务共存困境,如何实现Docker平滑升级?
  • 终极指南:3步掌握bilidown下载B站8K超清视频
  • ACadSharp终极指南:5个简单步骤掌握DXF/DWG文件处理
  • Cirq版本混乱导致项目崩溃?资深工程师教你构建可复现的补全开发环境
  • YOLOv8深度性能评测:全面解析FPS、延迟与多维度效率指标评估策略
  • (独家披露)大规模部署云原生Agent时,我们是如何实现Docker资源零浪费的
  • 为什么你的MCP网关总是失控?,深度解析Docker监控盲区与应对策略
  • **YOLOv12低照度检测革新:将SCINet作为可训练预处理主干的全链路指南
  • 为什么你的多模态Agent测试总失败?Docker环境变量配置的4个致命误区
  • 【量子开发工程师私藏技巧】:高效完成VSCode硬件状态检测的6种方式
  • 【量子电路可视化交互操作全解析】:掌握5大核心技巧提升研发效率
  • 揭秘Q#与Python混合编程:如何实现高效代码导航与智能跳转
  • 【VSCode量子开发必备技能】:深度挖掘历史记录中的隐藏数据
  • 高效获取Bandcamp音乐资源的完整实用指南
  • 从AutoGen到Microsoft Agent Framework:3步完成平滑迁移的技术指南
  • 基于web的酒店点餐系统的设计与实现申报表
  • SFC中文游戏和特辑攻略全5册 | PDF+图包