自制一个滚动条
参考文章:【https://blog.csdn.net/u014205965/article/details/45972803】
1 <html> 2 <head> 3 <title>自定义滚动条</title> 4 <style> 5 #parent{ 6 width: 400px; 7 height: 30px; 8 background: lightgrey; 9 position: relative; 10 } 11 12 #child{ 13 width: 30px; 14 height: 30px; 15 background: greenyellow; 16 position: absolute; 17 } 18 #box { 19 width: 0; 20 height: 0; 21 background: blueviolet; 22 } 23 </style> 24 </head> 25 <body> 26 <div id="parent"> 27 <div id="child"></div> 28 </div> 29 <div id="box"></div> 30 <script> 31 32 window.onload=function() { 33 var oParent = document.getElementById('parent'); 34 var oChild = document.getElementById('child'); 35 var disX = 0; 36 oChild.onmousedown = function (ev) { 37 var ev = ev || window.event; 38 disX = ev.clientX - oChild.offsetLeft; 39 document.onmousemove = function (ev) { 40 var ev = ev || window.event; 41 var oBox = document.getElementById('box'); 42 // 限制小Div拖动范围 43 var L = ev.clientX - disX; 44 45 if (L<0) { 46 //因为小div是相对大div定位的,所以当拖到大div的最左边的时候,小div的left就为0了 47 L = 0; 48 } 49 50 if (L > oParent.offsetWidth - oChild.offsetWidth) { 51 L = oParent.offsetWidth - oChild.offsetWidth; 52 } 53 54 // 定义一个滚动的比例,因为L的大小是由上面的判断语句决定的,所以scale需要定义在判断语句下面,定义在上面会出问题 55 var scale = L/(oParent.offsetWidth - oChild.offsetWidth); 56 oChild.style.left = L + 'px'; 57 oBox.style.width = scale * 300 + 'px'; 58 oBox.style.height = scale * 300 + 'px'; 59 60 } 61 document.onmouseup = function () { 62 document.onmousemove = null; 63 document.onmouseup = null; 64 } 65 return false; 66 } 67 } 68 </script> 69 </body> 70 </html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义