<body>
<div class="box"></div>
<script>
var box = document.getElementsByTagName("div")[0];
box.onmousedown = function(e){
var e = e || event;
var oldX = box.offsetLeft;
var oldY = box.offsetTop;
var oldmouseX = e.clientX;
var oldmouseY = e.clientY;
box.onmousemove = function(e){
var e = e || event;
var curX = e.clientX;
var curY = e.clientY;
this.style.left = oldX+(curX-oldmouseX)+"px";
this.style.top = oldY+(curY-oldmouseY)+"px";
}
document.onmouseup = function(){
box.onmousemove = null;
}
}
</script>
</body>
网友评论