<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> * { margin: 0; padding: 0; } .box { width: 300px; height: 300px; background: green; border: solid 1px; position: absolute; } </style> <script> var DOM, oX, oY; function tz(dom) { DOM = dom; } function un_tz() { DOM = null; } window.onmousemove = function(e) { var eve = e || window.event; if(DOM) { DOM.style.left = DOM.offsetLeft + eve.clientX - oX + 'px'; DOM.style.top = DOM.offsetTop + eve.clientY - oY + 'px'; } oX = eve.clientX; oY = eve.clientY; } </script> </head> <body> <div class="box" onmousedown="tz(this);" onmouseup="un_tz();"></div> </body> </html>