最近在公司很忙,没什么时间更新,忙中抽时间做了一个JS滚动条,因为火狐浏览器与谷歌浏览器的滚动条自定义样式过于麻烦,所以我打算自己写个方便改样式的滚动条
css
| |
| <style> |
| #CheShi { |
| background: #ccc; |
| width: 80%; |
| height: 200px; |
| overflow: hidden; |
| position: relative; |
| |
| moz-user-select: -moz-none; |
| -moz-user-select: none; |
| -o-user-select: none; |
| -khtml-user-select: none; |
| -webkit-user-select: none; |
| -ms-user-select: none; |
| user-select: none; |
| } |
| |
| #CheShi_ul { |
| margin: 0px; |
| padding: 0px; |
| position: absolute; |
| top: 0px; |
| left: 0px; |
| } |
| </style> |
html
| <div id="CheShi"> |
| <ul id="CheShi_ul"> |
| <li>测试(Start)</li> |
| <li>测试1</li> |
| <li>测试2</li> |
| <li>测试3</li> |
| <li>测试4</li> |
| <li>测试5</li> |
| <li>测试</li> |
| <li>测试</li> |
| <li>测试</li> |
| <li>测试</li> |
| <li>测试</li> |
| <li>测试</li> |
| <li>测试</li> |
| <li>测试</li> |
| <li>测试</li> |
| <li>测试(END)</li> |
| </ul> |
| </div> |
js
| |
| document.querySelectorAll("#CheShi")[0].innerHTML += |
| '<div id="Y_CustomScrollBar" style="height:100%;width:30px;position: absolute;right: 0px;top:0px;background:#666"><span style="width:80%;height: 50px;background:#999;display: block;border-radius: 10px;transform: translateX(-50%);position: absolute;left: 50%;top: 0px;"></span></div>'; |
| var CheShi = document.querySelectorAll("#CheShi")[0]; |
| var CheShiU = document.querySelectorAll("#CheShi #CheShi_ul")[0]; |
| var Y_CustomScrollBar_span = document.querySelectorAll("#Y_CustomScrollBar span")[0]; |
| var CheShiH = CheShi.offsetHeight; |
| var CheShiUH = CheShiU.offsetHeight; |
| var XJValue = CheShiUH - CheShiH; |
| var XJValue2 = CheShiH - XJValue; |
| var ExtraHeight = 0; |
| var DownY = 0; |
| var DownB = false; |
| |
| |
| if (XJValue2 > 50 && XJValue2 < CheShiH) { |
| Y_CustomScrollBar_span.style.height = XJValue2 + "px"; |
| } else if (XJValue2 < 50) { |
| ExtraHeight = XJValue2 - 50; |
| Y_CustomScrollBar_span.style.height = 50 + "px"; |
| } else { |
| document.querySelectorAll("#Y_CustomScrollBar")[0].style.display = "none" |
| } |
| |
| Y_CustomScrollBar_span.onmousedown = function(e1) { |
| DownY = e1.offsetY; |
| DownB = true; |
| } |
| Y_CustomScrollBar_span.onmouseup = function(e1) { |
| DownB = false; |
| } |
| |
| Y_CustomScrollBar_span.onmousemove = function(e2) { |
| if (DownB == true) { |
| |
| if (((e2.clientY - DownY)) >= 0 && ((e2.clientY - DownY)) <= CheShiH - parseInt(Y_CustomScrollBar_span.style.height)) { |
| Y_CustomScrollBar_span.style.top = "" + (e2.clientY - DownY) + "px"; |
| } |
| |
| CheShi_ul.style.top = -(parseInt(Y_CustomScrollBar_span.style.top) * ((CheShiUH - CheShiH) / (CheShiH - parseInt(Y_CustomScrollBar_span.style.height)))) + "px" |
| } |
| } |
效果


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南