news 2026/5/26 8:35:01

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

作者头像

张小明

前端开发工程师

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/5/26 4:41:52

DeepChem分子特征工程:三大方法对比与实战选择指南

DeepChem分子特征工程&#xff1a;三大方法对比与实战选择指南 【免费下载链接】deepchem Democratizing Deep-Learning for Drug Discovery, Quantum Chemistry, Materials Science and Biology 项目地址: https://gitcode.com/gh_mirrors/de/deepchem 引言&#xff1a…

作者头像 李华
网站建设 2026/5/26 4:37:36

FreeRTOS 的核心优势:四大特性

FreeRTOS 的核心优势&#xff1a;四大特性 系列文章目录 什么是FreeRTOS&#xff1f;为什么它是嵌入式开发的首选&#xff1f; 文章目录 FreeRTOS 的核心优势&#xff1a;四大特性一、内存占用小&#xff1a;极致优化应对资源受限二、支持多种硬件平台&#xff1a;跨架构适配三…

作者头像 李华
网站建设 2026/5/25 6:16:21

NideShop电商系统:5分钟快速搭建完整在线商城终极指南

想要快速拥有一个功能完善的在线商城吗&#xff1f;NideShop电商系统正是你需要的终极解决方案&#xff01;这个基于Node.js和React的开源项目为开发者提供了完整的电商平台&#xff0c;从商品展示到订单管理&#xff0c;从支付接口到物流跟踪&#xff0c;一应俱全。 【免费下载…

作者头像 李华
网站建设 2026/5/26 5:37:29

【SRE专家亲授】:Docker MCP 网关监控面板的7大核心组件详解

第一章&#xff1a;Docker MCP 网关监控面板概述Docker MCP&#xff08;Microservice Control Panel&#xff09;网关监控面板是一款专为微服务架构设计的可视化管理工具&#xff0c;集成于 Docker 容器化环境中&#xff0c;用于实时监控 API 网关的请求流量、服务健康状态、响…

作者头像 李华
网站建设 2026/5/26 5:38:02

揭秘VSCode远程调试卡顿问题:3步实现毫秒级响应的优化方案

第一章&#xff1a;VSCode远程调试卡顿问题的现状与影响在现代软件开发中&#xff0c;VSCode凭借其轻量级和强大的插件生态&#xff0c;成为开发者广泛使用的代码编辑器之一。然而&#xff0c;当通过Remote-SSH、Remote-WSL或Remote-Containers等扩展进行远程开发时&#xff0c…

作者头像 李华