DRAG-N-DROP Example using Javascript

This a simple example of drag n drop using javascript.


<!-- TWO STEPS TO INSTALL DRAG-N-DROP:

1. Copy the coding into the HEAD of your HTML document
2. Add the last code into the BODY of your HTML document -->

<!-- STEP ONE: Paste this code into the HEAD of your HTML document -->



<HEAD>

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
N = (document.all) ? 0 : 1;
var ob;
function MD(e) {
if (N) {
ob = document.layers[e.target.name];
X=e.x;
Y=e.y;
return false;
}
else {
ob = event.srcElement.parentElement.style;
X=event.offsetX;
Y=event.offsetY;
}
}
function MM(e) {
if (ob) {
if (N) {
ob.moveTo((e.pageX-X), (e.pageY-Y));
}
else {
ob.pixelLeft = event.clientX-X + document.body.scrollLeft;
ob.pixelTop = event.clientY-Y + document.body.scrollTop;
return false;
}
}
}
function MU() {
ob = null;
}

if (N) {
document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
}
document.onmousedown = MD;
document.onmousemove = MM;
document.onmouseup = MU;
// End -->
</script>
</HEAD>



<!-- STEP TWO: Copy this code into the BODY of your HTML document -->



<BODY>

<div id="s" style="position:absolute;left:50;top:300;">
<img src=~/img/cards/spades.gif name="s">
</div>

<div id="d" style="position:absolute;left:50;top:350;">
<img src=~/img/cards/diamonds.gif name="d">
</div>

<div id="c" style="position:absolute;left:100;top:300;">
<img src=~/img/cards/clubs.gif name="c">
</div>

<div id="h" style="position:absolute;left:100;top:350;">
<img src=~/img/cards/hearts.gif name="h">
</div>

Just click and hold the 4 suit images to the left and then you can move them around the page!

</body>
</html>


Comments

Author: Amaresh Chandra Das29 May 2009 Member Level: Bronze   Points : 0

owo.
good article!!!


Thanks,
-Amaresh

Author: Praveen Pula05 Jan 2010 Member Level: Gold   Points : 1

That was a good article. i was looking for dragging a div tag. if you have any suggestion on this would be a great help

Author: Hafiz Ayaz Mehmood27 Feb 2010 Member Level: Bronze   Points : 0



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: