<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /> <title>鼠标拖动小方块</title> <style type="text/css"> .lineDiv { position: relative; height: 5px; background: red; width: 95%; margin: 50px auto; } .lineDiv .minDiv { position: absolute; top: -15px; left: 0; width: 35px; height: 35px; background: green; cursor: pointer; transition: all 0s; } .lineDiv .vals { z-index: 100; position: absolute; top: 0px; left: 0px; width: 0px; height: 5px; background: blue; } </style> </head> <body> <center> <h3>用鼠标拖动小方块<span>0</span>%</h3> </center> <div> <div></div> <!-- --> <div></div> <!-- --> </div> <script> window.onload = function() { var lineDiv = document.getElementById('lineDiv'); //长线条 var minDiv = document.getElementById('minDiv'); //小方块 var minVals = document.getElementById('vals'); //左长线 var msg = document.getElementById("msg"); //最上面的信息 var ifBool = false; //判断滑块是否按下 var lineDiv_W = getPosition(lineDiv).width; //长线的长度 var lineDiv_L = getPosition(lineDiv).left; //长线距离html的left var minDiv_W = getPosition(minDiv).width; //滑块的长度 var minDiv_L = getPosition(minDiv).left; //滑块距离html的left var Slider_W_MAX = lineDiv_W - minDiv_W; //滑块可以滑动的最大值px,范围是0~Slider_W_MAX var minNum = 0; //最小值 var maxNum = 500; //最大值 var startNum = 100; //起始值 var endNum = 400; //结束值 var min_Px = Slider_W_MAX / maxNum * startNum; //滑块可以滑动的最小px var max_Px = Slider_W_MAX / maxNum * endNum; //滑块可以滑动的最大px var minDiv_left=0; //当前滑块的位置 /* Slider_W_MAX 1元对应的px? 1 maxNum 1 1px对应的金额? */ function initSlider(initPX) { //设置滑块的初始位置 console.log(initPX); minDiv_left=initPX; //设置滑块的位置 minDiv.style.left = initPX + "px"; minVals.style.width = initPX + "px"; msg.innerText = parseInt(initPX / Slider_W_MAX * 100); } (function() { //初始化滑块位置 if(startNum >= 0) { //求出startNum对应的px initSlider(Slider_W_MAX / maxNum * startNum) } })() //事件 var start = function(e) { ifBool = true; //console.log("鼠标按下") } var move = function(e) { //console.log("鼠标拖动") if(ifBool) { var x; //记录滑块距离html的距离left if(!e.touches) { //兼容PC端 x = e.clientX; } else { //兼容移动端 x = e.touches[0].pageX; } minDiv_left = x - lineDiv_L; //小方块相对于父元素(长线条)的left值 if(minDiv_left >= Slider_W_MAX) { minDiv_left = Slider_W_MAX; } if(minDiv_left < 0) { minDiv_left = 0; } //设置拖动后小方块的left值 initSlider(minDiv_left) } } var end = function(e) { if(minDiv_left>max_Px){ initSlider(max_Px); } if(minDiv_left<min_Px){ initSlider(min_Px); } ifBool = false; } //鼠标按下方块 minDiv.addEventListener("touchstart", start); minDiv.addEventListener("mousedown", start); //拖动 window.addEventListener("touchmove", move); window.addEventListener("mousemove", move); //鼠标松开 window.addEventListener("touchend", end); window.addEventListener("mouseup", end); //获取元素的绝对位置 function getPosition(node) { var width = node.offsetWidth; //元素宽度 var height = node.offsetHeight; //元素高度 var left = node.offsetLeft; //获取元素相对于其根元素的left值var left var top = node.offsetTop; //获取元素相对于其根元素的top值var top current = node.offsetParent; // 取得元素的offsetParent // 一直循环直到根元素 while(current != null) { left += current.offsetLeft; top += current.offsetTop; current = current.offsetParent; } return { "width": width, "height": height, "left": left, "top": top }; } } </script> </body> </html>
点击水波纹效果: