可以拖动的div

function beginDrag(elementToDrag,event) 

var deltaX=event.clientX-parseInt(elementToDrag.style.left); 
var deltaY=event.clientY-parseInt(elementToDrag.style.top); 
if(document.addEventListener) 

document.addEventListener(”mousemove”,moveHandler,true); 
document.addEventListener(”mouseup”,upHandler,true); 
//document.addEventListener(”mouseout”,upHandler,true); 

else if(document.attachEvent) 

document.attachEvent(”onmousemove”,moveHandler); 
document.attachEvent(”onmouseup”,upHandler); 
//document.attachEvent(”onmouseout”,upHandler); 

if(event.stopPropagation) event.stopPropagation(); 
else event.cancelBubble=true; 
if(event.preventDefault) event.preventDefault(); 
else event.returnValue=false; 
function moveHandler(e) 

  if (!e) e=window.event; //如果是IE的事件对象,那么就用window.event 
  //全局属性,否则就用DOM二级标准的Event对象。 
elementToDrag.style.left=(e.clientX-deltaX)+”px”; 
elementToDrag.style.top=(e.clientY-deltaY)+”px”; 
if(e.stopPropagation) e.stopPropagation(); 
else e.cancelBubble=true; 

function upHandler(e) 

if(document.removeEventListener) 

document.removeEventListener(”mouseup”,upHandler,true); 
document.removeEventListener(”mousemove”,moveHandler,true);} 
else 

document.detachEvent(”onmouseup”,upHandler); 
document.detachEvent(”onmousemove”,moveHandler);} 

if (!e) e=window.event; 
if(e.stopPropagation) e.stopPropagation(); 
else e.cancelBubble=true; 

你可能感兴趣的:(html,js)