Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Moving a Div Tag using Mouse Drag 2

Status
Not open for further replies.

fixthebug2003

Programmer
Oct 20, 2003
294
US
Hi,
Is it possible to Drag a Div tag using the mouse. Lets say I have a <DIV> tag with a couple of Form Elements...Is it possible to use the mouse, click and Drag the div tag ..like you would do a simple window?

Fixthebug2003
 
Here's what I wrote for my sites and it works quite well:
Code:
<script>
<!--
var MD;
var MX;
var MY;

document.onmouseup = DragOff;

function DragOn() {
	if (event.button == 1) {
		MD = true;
		MX = event.offsetX;
		MY = event.offsetY;
	}
}

function DragOff() {
	MD = false;
}

function DragMe() {
	if (MD == true) {
		test.style.pixelLeft = (event.clientX - MX) + document.body.scrollLeft;
		test.style.pixelTop = (event.clientY - MY) + document.body.scrollTop;
	}
}
//-->
</script>

<div id="test" style="position:absolute; border:1px solid #000000">
<table>
<tr>
<td align="left" valign="middle" bgColor="#0000FF" style="color:#FFFFFF; cursor:default" onmousedown="DragOn()" onmouseup="DragOff()" onmousemove="DragMe()" onselectstart="return false" onmouseout="DragOff()">
Drag this titlebar
</td>
</tr>
<tr>
<td align="center" valign="middle">
Hi, I'm a draggable box.
</td>
</tr>
</table>
</div>
 
supra --- nice!

;)

__________________________________________________________
"The only difference between me and a mad man is that I'm not mad."
- Dali
 
oops you gave the star to me instead...lol

______________________________________________________________________________
"The sluggard does not plow after the season, so he begs during the harvest and has nothing."
-[Proverbs 20:4]


 
Yep, Dan is right. I primarily develop for IE users so my work can't always be trusted ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top