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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can web form perform drag and drop? 1

Status
Not open for further replies.

adonet

MIS
May 4, 2004
312
US
Can web form perform drag and drop?
 
Using client side javascript, yes.
 
Just stumbled across this post.
I'd be interested in seeing the javascript that enables the dragging and dropping principle. :)
 
Here is a small sample that will allow to drag and move a text box (works in IE). Drop it wherever you want :).
Code:
<html>
<head>
	<title>Drag and Drop</title>	
</head>

<script language="javascript">
	var DragEl, InitLeft, InitTop, PrevBorder;
	var DragGrain    = 1;
	var DragSynch    = false;
	var intLeft      = 10;
	var intTop       = 10;	

	if (document.all){
		document.onmousedown = DragStart;
		document.onmouseup   = DragEnd;
		document.onmousemove = DoDrag;
	}

	function DragStart() 
	{
		el = event.srcElement;
		if ( el.className == "dndWin" ) {
			DragEl=el;
			DragGrain=1;
			while (DragEl.tagName != "DIV") DragEl = DragEl.parentElement;
			InitLeft=event.clientX-DragEl.style.pixelLeft;
			InitTop=event.clientY-DragEl.style.pixelTop;
			DragSynch = false;
			
			el.style.cursor = "move";
		}
	}

	function DragEnd()
	{
		if ( DragEl ) {
			DragEl = null;
			el.style.cursor = "default";
		}
	}

	function DoDrag(){
		if(DragEl){
			if ( DragSynch ) {
				var newX = event.clientX;
				var newY = event.clientY;
			}
			else {
				var newX = event.clientX-InitLeft;
				var newY = event.clientY-InitTop;
			}
			if ( (newX % DragGrain) == 0 )
				DragEl.style.pixelLeft=newX;
			if ( (newY % DragGrain) == 0 )
				DragEl.style.pixelTop=newY;
			return false;
		}
	}
</script>

<body bgcolor="#cccccc">
	<center>																		
		<div id="divDnD" position:absolute;left:550;top:100;">
			<input type=text name="txtDnD" class="dndWin">
		</div>						
	</center>				
</body>
</html>
 
Sorry, the HTML should read:
Code:
<body bgcolor="#cccccc">           
   <div id="divDnD" style="position:absolute;left:550;top:100;">
       <input type=text name="txtDnD" class="dndWin">
   </div>                           
</body>
 
It works. Can we drag a item within one data list and then drop to another data list?
 
The script posted just provides a general idea on how to drag controls around the screen. I guess, you can become creative and make it work with control collections, such as items in a list boxe. I haven't tried.
 
I want to do something like this for what I'm doing, too. Star from me.
 
BoulderBum: thanks for the star. The code is pretty raw, it was written as a test awhile ago and I don't think it was tested in other than IE browsers.
 
Yeah, I'm learning the mouse event argument stuff is a little different for Netscape, etc., but it's a good demonstration of the concept at least.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top