Click here to Skip to main content
15,880,469 members
Articles / Programming Languages / Javascript

JavaScript example of drag and drop to a target

Rate me:
Please Sign up or sign in to vote.
4.88/5 (6 votes)
21 Apr 2012CPOL1 min read 144.6K   2.4K   21   4
This article explains how to make drag and drop to a target work across multiple browsers.

Introduction

This article is a sequel to the article I wrote earlier: Cross-browser drag and drop. In that article, I provided a JavaScript example of drag and drop. Here I would like to provide a JavaScript example of drag and drop to a target that would work in all browsers (including Safari-iPad).

This article is for those that like nuts and bolts and are not using third party libraries like jQuery. There is nothing wrong with using third party libraries – I just like more flexibility. For those that like nuts and bolts I would recommend this third party library: Redips.

366432/DragDrop.gif

Using the code

To use this script, use the SetupDragDrop JavaScript function. It will make some DIVs dragable and other DIVs drag targets based on the CSS class (Dragable or DropTarget) used. Please note that this example shows how to “capture” the element so that you don’t lose it if you move outside of the element or outside of the browser window. Here is the full JavaScript code:

JavaScript
var oDragTargets = [];
var oDragTarget = null;
var oDragItem = null;
var iClickOffsetX = 0;
var iClickOffsetY = 0;

function OnLoad(){
	SetupDragDrop();
}

function SetupDragDrop(){
	oDragTargets = [];
	
	var oList = document.getElementsByTagName("div");
	for(var i=0; i<olist.length; y="e.clientY" x="e.clientX" otarget="oDragTargets[i];" i="0;" top="y;" left="x;" position="absolute" zindex="1000;" e="window.event;" (odragitem="=null)" iclickoffsety="e.targetTouches[0].pageY" iclickoffsetx="e.targetTouches[0].pageX" opos="GetObjPos(e.target);" odragitem="o;" önmousedown="function(e){DragStart(oBox,e);return" obox.onmouseup="function(e){DragStop(oBox,e)};" obox.onmousemove="function(e){DragMove(oBox,e)};" obox.ontouchend="function(e){TouchEnd(e)};" obox.ontouchmove="function(e){TouchMove(e)};" obox.ontouchstart="function(e){TouchStart(e)};" (navigator.platform="="iPad"){" (o.classname="=" odragtargets[odragtargets.length]="GetObjPos(o);" o="oList[i];"> x && (oTarget.y + oTarget.h) > y){
			if (oDragTarget!=null && oDragTarget != oTarget.o) OnTargetOut();
			oDragTarget = oTarget.o;
			OnTargetOver();
			return;
		}
	}
	
	if (oDragTarget){
		OnTargetOut();
		oDragTarget = null;
	}
}

function TouchMove(e){
    e.preventDefault();
    var x = e.targetTouches[0].pageX - iClickOffsetX;
    var y = e.targetTouches[0].pageY - iClickOffsetY;
    oDragItem = e.targetTouches[0].target;
    HandleDragMove(x,y);
}

function DragStop(o,e){
	if (o.releaseCapture){
		o.releaseCapture();
	}else if (oDragItem){
		window.removeEventListener ("mousemove", DragMove2, true);
		window.removeEventListener ("mouseup",   DragStop2, true);
	}
	
	HandleDragStop();
}

function HandleDragStop(){
	if (oDragItem==null) return;

	if (oDragTarget){
		OnTargetOut();
		OnTargetDrop();
		oDragTarget = null;
	}
	
	oDragItem.style.zIndex = 1;
	oDragItem = null;
}

function TouchEnd(e){
	e.target.innerHTML = "TouchEnd";
	HandleDragStop();
}

function $(s){
	return document.getElementById(s);
}

function GetObjPos(obj){
	var x = 0;
	var y = 0;
	var o = obj;
	
	var w = obj.offsetWidth;
	var h = obj.offsetHeight;
	if (obj.offsetParent) {
		x = obj.offsetLeft
		y = obj.offsetTop
		while (obj = obj.offsetParent){
			x += obj.offsetLeft;
			y += obj.offsetTop;
		}
	}
	return {x:x, y:y, w:w, h:h, o:o};
}

//Drag and Drop Events
function OnTargetOver(){
	oDragTarget.style.border = "3px solid red";
}

function OnTargetOut(){
	oDragTarget.style.border = "";
}

function OnTargetDrop(){
	oDragItem.style.position="";
	oDragTarget.appendChild(oDragItem);
	if (navigator.platform=="iPad") MakeDragable(oDragItem);
}

</olist.length;>

I created two CSS classes to make drag-able elements not selectable and to have the cursor suggest that the element is drag-able.

CSS
.Dragable{
    cursor:move;
   -moz-user-select: -moz-none;   
   -khtml-user-select: none;   
   -webkit-user-select: none;   
   -o-user-select: none;   
   user-select: none;
   width: 100px; 
   height:20px; 
   padding: 3px;
}

.DropTarget{
    width: 200px; 
    height:200px; 
    background-color: LightBlue;
    border: 3px solid white;
}

I hope someone might find this example useful.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Igor is a business intelligence consultant working in Tampa, Florida. He has a BS in Finance from University of South Carolina and Masters in Information Management System from University of South Florida. He also has following professional certifications: MCSD, MCDBA, MCAD.

Comments and Discussions

 
QuestionDrag Drop Javascript Pin
Daniel H Oostra19-Jun-12 7:02
Daniel H Oostra19-Jun-12 7:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.