Click here to Skip to main content
Click here to Skip to main content

Cross-browser drag and drop

By , 21 Apr 2012
 

Introduction

I could not find a good Drag and Drop JavaScript example that would work in all browsers (including iPad). So I wrote one myself. I hope someone might find it useful. 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.

Unfortunately, I could not find a good way how not to lose capture over an iFrame in Chrome and Safari. The best approach I found is to cover all iFrames with DIVs while the dragging is taking place. Any suggestions on how to do this better are welcome.

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 example, you can use this example to resize elements.

Using the code

To use this script, use the MakeDragable JavaScript function. It accepts only one parameter: the ID of the element you want users to drag. Here is the full JavaScript code:


var oDragItem = null;
var iClickOffsetX = 0;
var iClickOffsetY = 0;

function OnLoad(){
	MakeDragable("idBox1");
	MakeDragable("idBox2");	
}

function MakeDragable(id){
	var oBox = $(id);
	oBox.className = "Dragable";
	
	if (navigator.platform=="iPad"){
		oBox.ontouchstart= function(e){TouchStart(e)};
		oBox.ontouchmove=function(e){TouchMove(e)};
	}else{
		oBox.onmousemove= function(e){DragMove(oBox,e)};
		oBox.onmouseup=function(e){DragStop(oBox,e)};
		oBox.onmousedown=function(e){DragStart(oBox,e);return false};	
	}	
}

function TouchStart(e){
	var oPos = GetObjPos(e.target);
	iClickOffsetX = e.targetTouches[0].pageX - oPos.x;
	iClickOffsetY = e.targetTouches[0].pageY - oPos.y;
}

function DragStart(o,e){
	if(!e) var e = window.event;
	oDragItem = o;
	
	if (e.offsetX){
		iClickOffsetX = e.offsetX;
		iClickOffsetY = e.offsetY;	
	}else{
		var oPos = GetObjPos(o);
		iClickOffsetX = e.clientX - oPos.x;
		iClickOffsetY = e.clientY - oPos.y;
	}
	
	if (o.setCapture){
		o.setCapture();
	}else{
		window.addEventListener ("mousemove", DragMove2, true);
		window.addEventListener ("mouseup",   DragStop2, true);
	}
}

function DragMove2(e){
	DragMove(oDragItem,e);
}

function DragStop2(e){
	DragStop(oDragItem,e);
}

function DragMove(o,e){
	if (oDragItem==null) return;

	if(!e) var e = window.event;
	var x = e.clientX + document.body.scrollLeft - document.body.clientLeft - iClickOffsetX;
	var y = e.clientY + document.body.scrollTop  - document.body.clientTop - iClickOffsetY;
	
	with(oDragItem.style){
		zIndex = 1000;
		position="absolute";
		left=x;
		top=y;
	}
}

function TouchMove(e){
    e.preventDefault();
    var curX = e.targetTouches[0].pageX - iClickOffsetX;
    var curY = e.targetTouches[0].pageY - iClickOffsetY; 
    
    var o = e.targetTouches[0].target;
    o.style.position = "absolute";
    o.style.top  = curY + 'px'; 
    o.style.left = curX + 'px'; 
}

function DragStop(o,e){
	if (oDragItem==null) return;

	if (o.releaseCapture){
		o.releaseCapture();
	}else if (oDragItem){
		window.removeEventListener ("mousemove", DragMove2, true);
		window.removeEventListener ("mouseup",   DragStop2, true);
	}
	
	oDragItem.style.zIndex = 1;
	oDragItem = null;
}

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

function GetObjPos(obj){
	var x = 0;
	var y = 0;
	
	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};
}


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

<style>
.Dragable
{
   cursor:move;
   -moz-user-select: -moz-none;   
   -khtml-user-select: none;   
   -webkit-user-select: none;   
   -o-user-select: none;   
   user-select: none;
}

Points of Interest

I also wrote a related article called: JavaScript example of drag and drop to a target

License

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

About the Author

Igor Krupitsky
Web Developer
United States United States
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionAndroidmemberwerd6 Jul '12 - 5:42 
Since android has a huge market share, this cannot be called cross-browser if it does not work in "ANY" android broswer
GeneralMy vote of 5memberIvan Stefanov18 Jul '11 - 22:17 
Everyone is free to use whatever he want. I think the purpose of this article is to show how you can make your own implementation of drag and drop. For me it's always better to use something yours for small tasks instead to use libraries or somebody else's source.
GeneralRe: My vote of 5membersecorbett21 Jul '11 - 2:16 
I agree with this 100%. I do not agree with the author's premise that there aren't any good cross-browser implementations of drag and drop. There are plenty of them.
Scott E. Corbett
Software Engineer/Analyst

GeneralRe: My vote of 5memberIgor Krupitsky16 Aug '11 - 13:22 
Scott,
 
I did not say there were not good cross-browser implementations. I just could not find them. If you can find them please provide links.
 
Igor
GeneralMy vote of 3membersecorbett18 Jul '11 - 9:12 
Good article, but there are already any number of frameworks available to handle this.
SuggestionGood Attempt - but why not jQuery?memberrManiks15 Jul '11 - 4:05 
Good attempt - but there are a number of jQuery plugins available for this.
for e.g. Draggable demo[^]
GeneralRe: Good Attempt - but why not jQuery?membersecorbett18 Jul '11 - 9:12 
I have to agree with this. jQuery is the most popular framework that has cross-browser support for drag and drop. There are a number of others as well.
Scott E. Corbett
Software Engineer/Analyst

GeneralRe: Good Attempt - but why not jQuery?memberDewey20 Jul '11 - 22:52 
Wow, so now it's not ok to use the actual framework of the browser?
 
Let me know when Bell Bottoms come back in style, so I can purchase some!
GeneralRe: Good Attempt - but why not jQuery?membersecorbett21 Jul '11 - 2:14 
"I could not find a good Drag and Drop JavaScript example that would work in all browsers." First, jQuery is using the framework of the browser. Second, it works in all browsers. Third, how in the world is using a framework like going back in time? In short, your basic reason for the code you wrote was flawed. I don't have any problems with the code itself, but really, don't try to make it out as an original solution to an existing problem.
Scott E. Corbett
Software Engineer/Analyst

GeneralRe: Good Attempt - but why not jQuery?memberDewey20 Jul '11 - 22:50 
First of all, it's not an attempt, he accomplished his goal!
 
Second, there's nothing wrong with NOT using jQuery, obviously he didn't need the crutch.
GeneralRe: Good Attempt - but why not jQuery?memberHaBiX24 Jul '11 - 23:11 
I thought he made drag & drop across 2 open browser windows.. (its early in morning & i'm really not awake yet)
 

for everything else.. jquery. this "cross-browser" solution will only work until MS releases next IE Poke tongue | ;-P
 

good article anyway - shows how its done even under jquery
GeneralRe: Good Attempt - but why not jQuery?memberwerd6 Jul '12 - 5:20 
Actually, it does not work in the native android browser, so yes, "Attempt" is correct.
GeneralRe: Good Attempt - but why not jQuery?memberwerd6 Jul '12 - 5:25 
also does not work in opera mini

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 21 Apr 2012
Article Copyright 2011 by Igor Krupitsky
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid