x1等于div.offsetLeft
y1等于div.offsetTop
x2等于ev.clientX(ev表示event事件)
y2等于ev.clientY
当我们在方块上按下鼠标的时候,x2-x1即可确定。移动鼠标之后,我们用鼠标当前的位置即x4、y4减去x2-x1、y2-y1就可以得到方块现在的位置。
代码:
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><style> #box{width: 100px;height: 100px;background: red; position: absolute; }</style> </head> <body><div id="box"></div><script type="text/javascript">var oBox = document.getElementById("box");oBox.onmousedown = function(ev){// 鼠标按下 var ev = ev || event; // 获取鼠标离div得距离var mouseBoxleft = ev.clientX - this.offsetLeft;var mouseBoxTop = ev.clientY - this.offsetTop; oBox.onmousemove = function(ev){ // 鼠标按下左键并移动 var ev = ev || event;// 设置div移动时,它的位置 oBox.style.left = ev.clientX - mouseBoxleft + "px"; oBox.style.top = ev.clientY - mouseBoxleft + "px"; } oBox.onmouseup = function(){ // 鼠标左键抬起oBox.onmousemove = oBox.onmouseup = null; }}</script> </body> </html>优化代码:
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"> <title>Document</title><style> #box{ width: 100px;height: 100px; background: red;position: absolute; }</style> </head> <body><div id="box"></div><script> var oBox = document.getElementById("box");oBox.onmousedown = function(ev){// 鼠标按下 var ev = ev || event; // 获取鼠标离div得距离var mouseBoxleft = ev.clientX - this.offsetLeft;var mouseBoxTop = ev.clientY - this.offsetTop; document.onmousemove = function(ev){// 鼠标按下左键并移动 var ev = ev || event;// 设置div移动时,它的位置 oBox.style.left = ev.clientX - mouseBoxleft + "px"; oBox.style.top = ev.clientY - mouseBoxleft + "px";}document.onmouseup = function(){ // 鼠标左键抬起 document.onmousemove = document.onmouseup = null; }}</script> </body> </html>【2】当要拖动的方块中有文字时会触发浏览器的默认行为
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><style> #box{width: 100px;height: 100px;background: red;position: absolute;top: 0;left: 0;} </style> </head> <body><div id="box">模拟拖拽</div><script> var oBox = document.getElementById("box"); oBox.onmousedown = function(ev){ // 鼠标按下 var ev = ev || event; // 获取鼠标离div得距离 var mouseBoxleft = ev.clientX - this.offsetLeft; var mouseBoxTop = ev.clientY - this.offsetTop; document.onmousemove = function(ev){ // 鼠标按下左键并移动 var ev = ev || event;// 设置div移动时,它的位置oBox.style.left = ev.clientX - mouseBoxleft + "px";oBox.style.top = ev.clientY - mouseBoxleft + "px"; } document.onmouseup = function(){// 鼠标左键抬起document.onmousemove = document.onmouseup = null;} // 阻止默认行为 return false;}</script> </body> </html>2、使用全局捕获(IE)
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title> </head> <body><input type="button" id="button1" value="弹出1" /><input type="button" id="button2" value="弹出2" /><script type="text/javascript">window.onload = function(){var Btn1 = document.getElementById("button1"); var Btn2 = document.getElementById("button2");Btn1.setCapture();Btn1.onclick = function(){alert(1);} Btn2.onclick = function(){ alert(2);} } </script> </body></html>给Btn1设置了全局捕获之后,即使我们点击了Btn2还是会触发Btn1的点击事件
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><style>#box{width: 100px;height: 100px;background: red;position: absolute;}</style></head> <body><div id="box">模拟拖拽</div><script> var oBox = document.getElementById("box");oBox.onmousedown = function(ev){// 鼠标按下var ev = ev || event;// 获取鼠标离div得距离 var mouseBoxleft = ev.clientX - this.offsetLeft; var mouseBoxTop = ev.clientY - this.offsetTop; // IE浏览器,全局捕获 if(oBox.setCapture){ oBox.setCapture(); } document.onmousemove = function(ev){// 鼠标按下左键并移动 var ev = ev || event;// 设置div移动时,它的位置oBox.style.left = ev.clientX - mouseBoxleft + "px";oBox.style.top = ev.clientY - mouseBoxleft + "px"; } document.onmouseup = function(){ // 鼠标左键抬起 document.onmousemove = document.onmouseup = null;//IE下,释放全局捕获 releaseCapture(); if ( oBox.releaseCapture ) { oBox.releaseCapture();} } // 阻止默认行为 return false;}</script> </body> </html>【3】封装模拟拖拽函数
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><style> #box{width: 100px;height: 100px;background: red;position: absolute; }</style> </head> <body><div id="box">模拟拖拽</div><script>var oBox = document.getElementById("box");drag(oBox);function drag(obj){ obj.onmousedown = function(ev){// 鼠标按下 var ev = ev || event;// 获取鼠标离div得距离var mouseBoxleft = ev.clientX - this.offsetLeft;var mouseBoxTop = ev.clientY - this.offsetTop;// IE浏览器,全局捕获if(obj.setCapture){ obj.setCapture();}document.onmousemove = function(ev){// 鼠标按下左键并移动var ev = ev || event; // 设置div移动时,它的位置 obj.style.left = ev.clientX - mouseBoxleft + "px"; obj.style.top = ev.clientY - mouseBoxleft + "px";}document.onmouseup = function(){// 鼠标左键抬起document.onmousemove = document.onmouseup = null;//IE下,释放全局捕获 releaseCapture(); if ( obj.releaseCapture ) {obj.releaseCapture();}}// 阻止默认行为return false;} }</script> </body></html>以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!