news 2026/6/10 18:59:58

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
横向滚动上方列表查看进度条变化
<!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 中的导航辅助(如故事流、商品轮播)
  • 增强用户对可滚动区域长度的感知

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 15:55:58

构建失败紧急应对:Docker Buildx日志实时监控与诊断方案

第一章&#xff1a;构建失败紧急应对&#xff1a;Docker Buildx日志实时监控与诊断方案在多平台镜像构建场景中&#xff0c;Docker Buildx 成为关键工具。一旦构建失败&#xff0c;缺乏有效的日志监控机制将导致问题定位困难。通过合理配置日志输出与诊断策略&#xff0c;可实现…

作者头像 李华
网站建设 2026/6/10 6:09:42

如何快速获取SUSE Linux Enterprise系统:新手完整下载部署指南

如何快速获取SUSE Linux Enterprise系统&#xff1a;新手完整下载部署指南 【免费下载链接】SUSELinuxEnterprise1215系统下载指南 SUSE Linux Enterprise 12/15 系统下载指南欢迎来到SUSE Linux Enterprise系统资源下载页面 项目地址: https://gitcode.com/open-source-tool…

作者头像 李华
网站建设 2026/6/10 17:09:16

如何排错运行在Kubernetes集群中的服务?

我们的前端服务以job运行。k8s的job用于执行一次性的任务&#xff0c;运行完毕即退出。以job方式运行的前端服务启动程序只做一件事情&#xff0c;将镜像中指定目录下的静态文件上传到对象存储。上传前会做一些变量替换&#xff0c;替换的值和对象存储的连接信息存储在Nacos中。…

作者头像 李华
网站建设 2026/6/10 4:05:10

CosyVoice2流式语音合成中的音色一致性挑战与优化实践

CosyVoice2流式语音合成中的音色一致性挑战与优化实践 【免费下载链接】CosyVoice Multi-lingual large voice generation model, providing inference, training and deployment full-stack ability. 项目地址: https://gitcode.com/gh_mirrors/cos/CosyVoice 现象观察…

作者头像 李华
网站建设 2026/6/10 17:18:28

YOLOv9模型评估全透视:从性能解码到调优实战

YOLOv9模型评估全透视&#xff1a;从性能解码到调优实战 【免费下载链接】yolov9 项目地址: https://gitcode.com/GitHub_Trending/yo/yolov9 在目标检测领域&#xff0c;YOLOv9作为最新一代的实时检测模型&#xff0c;其评估过程不仅是验证性能的必要步骤&#xff0c;…

作者头像 李华
网站建设 2026/6/10 17:09:59

Taskflow:重新定义C++并行编程的新范式

Taskflow&#xff1a;重新定义C并行编程的新范式 【免费下载链接】taskflow 项目地址: https://gitcode.com/gh_mirrors/taskfl/taskflow 在现代计算环境中&#xff0c;并行编程已成为提升应用性能的关键技术。Taskflow作为一款基于现代C构建的开源并行编程框架&#x…

作者头像 李华