分享一个简单的拖拽
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <style> #box{width:100px;height:100px;background:red;text-align:center;line-height:100px;color:#fff;position:absolute;left:0;top:0;} </style> <script> window.onload = function (){ var oBox = document.getElementById('box'); var disX = 0; var disY = 0; oBox.onmousedown = function ( ev ){ var ev = ev || window.event; disX = ev.clientX - this.offsetLeft; disY = ev.clientY - this.offsetTop; document.onmousemove = function ( ev ){ var ev = ev || window.event; oBox.style.left = ev.clientX - disX + 'px'; oBox.style.top = ev.clientY - disY + 'px'; ev.preventDefault && ev.preventDefault(); return false; } document.onmouseup = function (){ this.onmousemove = this.onmouseup = null; } } } </script> </head> <body> 其他文字<br /> 其他文字<br /> 其他文字<br /> 其他文字<br /> 其他文字<br /> <div id="box">拖拽</div> </body> </html>