<script> var oBox = document.querySelector('.box') ;
// 拖拽 // 鼠标按下 // 获取鼠标距离盒子的位置 // 鼠标移动
// 鼠标抬起
oBox.onmousedown = function(e){ var e = event || e ; var gapx = e.x - oBox.offsetLeft ; var gapy = e.y - oBox.offsetTop ;
document.onmousemove = function(e) { var e = event || e ; var x = e.x - gapx; var y = e.y - gapy ;
oBox.style.cssText =`left:${x}px;top:${y}px` } }
document.onmouseup = function() { document.onmousemove = null } </script> </body> </html>