JavaScript:元素拖拽
/* 拖動元素函數 */ function Drag(element) { element.onmousedown = function(event) { var distanceX=event.clientX-this.offsetLeft; var distanceY=event.clientY-this.offsetTop; if(element.setCapture)element.setCapture(); document.onmousemove=function(event){ var distanceLeft=event.clientX-distanceX; var distanceTop=event.clientY-distanceY; if(distanceLeft<0){ distanceLeft=0; }else if (distanceLeft>document.documentElement.offsetWidth-element.offsetWidth){ distanceLeft=document.documentElement.offsetWidth-element.offsetWidth; } if(distanceTop<0){ distanceTop=0; }else if(distanceTop>document.documentElement.offsetHeight-element.offsetHeight){ distanceTop=document.documentElement.offsetHeight-element.offsetHeight; } element.style.left=distanceLeft+'px'; element.style.top=distanceTop+'px'; } document.onmouseup=function(){ document.onmousemove=document.onmouseup=null; if(element.releaseCapture)element.releaseCapture(); } return false; } }