var oDiv= document.getElementById("div"); oDiv.onmousedown= function(e){ var _this = this; var e= e || window.event; var diffX= e.clientX - _this.offsetLeft; var diffY= e.clientY - _this.offsetTop; if(_this.setCapture) _this.setCapture(); document.onmousemove= function(e){ var e= e || window.event; var left = e.clientX -diffX; var top= e.clientY -diffY; left= left < 0 ? 0 : left; left= left > document.documentElement.clientWidth - _this.offsetWidth ? document.documentElement.clientWidth - _this.offsetWidth : left; top= top < 0 ? 0 : top; top= top > document.documentElement.clientHeight - _this.offsetHeight ? document.documentElement.clientHeight - _this.offsetHeight : top; _this.style.left= left + "px"; _this.style.top= top + "px"; } document.onmouseup= function(e){ this.onmousemove= null; this.onmouseup= null; if(_this.releaseCapture) _this.releaseCapture(); } }