Can use the mouse to drag DIV ideas and code

  • 2020-03-26 21:35:09
  • OfStack

 
<html><head> 
<title> Test the movable div</title> 
<script language='javascript' type='text/javascript'> 
var offset_x; 
var offset_y; 
function Milan_StartMove(oEvent) 
{ 
var whichButton; 
if(document.all&&oEvent.button==1) whichButton=true; 
else { if(oEvent.button==0)whichButton=true;} 
if(whichButton) 
{ 
var oDiv=document.getElementById("oDiv"); 
offset_x=parseInt(oEvent.clientX-oDiv.offsetLeft); 
offset_y=parseInt(oEvent.clientY-oDiv.offsetTop); 
document.documentElement.onmousemove=function(mEvent) 
{ 
var eEvent; 
if(document.all) eEvent=event; 
else{eEvent=mEvent;} 
var oDiv=document.getElementById("oDiv"); 
var x=eEvent.clientX-offset_x; 
var y=eEvent.clientY-offset_y; 
oDiv.style.left=(x)+"px"; 
oDiv.style.top=(y)+"px"; 
} 
} 
} 
function Milan_StopMove(oEvent){document.documentElement.onmousemove=null; } 
</script> 
</head> 
<body> 
<div id="oDiv" onmousedown="Milan_StartMove(event)" onmouseup="Milan_StopMove(event)" 

style="cursor:move;position:absolute;width:100px;height:60px;border:1px solid 

silver;left:100px;top:100px;z-index:9999;"></div> 
</body></html> 

Document.all [] is an array variable composed of all the tags in the document, including all the elements in the document object.
Event. Button value: 0 no key 1 press left key 2 press right key 3 press left key and right key 4 press middle key 5 press left and middle key 6 press right and middle key 7 press all keys

Let's improve this code to mimic window and make it override select
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title> Test the movable div</title> 
<script language='javascript' type='text/javascript'> 
var offset_x; 
var offset_y; 
function Milan_StartMove(oEvent,div_id) 
{ 
var whichButton; 
if(document.all&&oEvent.button==1) whichButton=true; 
else { if(oEvent.button==0)whichButton=true;} 
if(whichButton) 
{ 
var oDiv=div_id; 
offset_x=parseInt(oEvent.clientX-oDiv.offsetLeft); 
offset_y=parseInt(oEvent.clientY-oDiv.offsetTop); 
document.documentElement.onmousemove=function(mEvent) 
{ 
var eEvent; 
if(document.all) eEvent=event; 
else{eEvent=mEvent;} 
var oDiv=div_id; 
var x=eEvent.clientX-offset_x; 
var y=eEvent.clientY-offset_y; 
oDiv.style.left=(x)+"px"; 
oDiv.style.top=(y)+"px"; 
var d_oDiv=document.getElementById("disable_"+oDiv.id); 
d_oDiv.style.left=(x)+"px"; 
d_oDiv.style.top=(y)+"px"; 
} 
} 
} 
function Milan_StopMove(oEvent){document.documentElement.onmousemove=null; } 
function div_Close(o) 
{var oDiv=o; oDiv.style.display="none";var d_oDiv=document.getElementById("disable_"+o.id);d_oDiv.style.display="none";} 
</script> 
</head> 
<body> 
<div id="oDiv" style="position:absolute;width:100px;height:60px;border:1px solid silver;left:100px;top:100px;z-index:9999;"> 
<div id="move" onmousedown="Milan_StartMove(event,this.parentNode)" onmouseup="Milan_StopMove(event)" 
style="cursor:move;width:100%;height:15px;background-color:#0066cc; font-size:10px;"> 
<div onclick="div_Close(this.parentNode.parentNode)" style="float:right; width:10px; height:100%; cursor:hand; background-color:#cc3333; color:White;font-size:15px;">X</div> 
</div> 
<span> Test the </span> 
</div> 
<div id="disable_oDiv" style="position:absolute;left:100px;top:100px;width:100px; height:60px; z-index:9998;FILTER:alpha(opacity=50);";> 
<iframe src="about:blank" name="hiddenIframe" width="100%" frameborder="0" height="60px" title=" Cover layer "></iframe></div> 
<select name="ListHead1$DropDownList3" id="ListHead1_DropDownList3"> 
<option selected="selected" value=""></option> 
<option value="2">3333</option> 
<option value="6">1111</option> 
<option value="B">222</option> 
</select> 
</body> 
</html> 

Isn't this draggable div much better now? Don't worry about select. What had previously worked only in IE, mostly with parentElement, I replaced it with parentNode and tweaked the CSS style so it worked with FF.

Related articles: