当前位置:网站首页 > 技术博客 > 正文

js clienty



<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { margin: 0; padding: 0; } div.container { position: relative; height: 20px; width: 400px; border-radius: 20px; margin: 100px auto; background-color: red; } div.scrollbar { position: absolute; height: 40px; left: -20px; top: -10px; width: 40px; border-radius: 50%; background-color: green; } </style> </head> <body> <div class="container"> <div class="scrollbar"></div> </div> <script> var container = document.querySelector('.container'); var scrollbar = document.querySelector('.scrollbar'); // 计算容器的offsetLeft var _left = container.offsetLeft; // 边界判断 var leftMin = -scrollbar.clientWidth / 2; var leftMax = container.clientWidth - scrollbar.clientWidth / 2; scrollbar.onmousedown = function () { document.onmousemove = function (e) { // 得到鼠标想对于容器container的左侧坐标 var left = e.clientX - _left; left = Math.min(Math.max(leftMin, left), leftMax); scrollbar.style.left = left + 'px'; } document.onmouseup = function () { this.onmousemove = null; } } </script> </body> </html>

版权声明


相关文章:

  • js点击事件没反应2024-12-06 23:30:05
  • linux ar -x2024-12-06 23:30:05
  • mysql触发器在哪2024-12-06 23:30:05
  • 背包问题的贪心算法所需的计算时间为2024-12-06 23:30:05
  • bind9配置详解2024-12-06 23:30:05
  • 代码对比合并工具2024-12-06 23:30:05
  • linux查看文件权限修改记录2024-12-06 23:30:05
  • 深度置信网络 特征提取2024-12-06 23:30:05
  • ftoa函数2024-12-06 23:30:05
  • vue启动后浏览器访问不了2024-12-06 23:30:05