Mouse Drag and Drop : Mouse Event « Event « JavaScript DHTML






Mouse Drag and Drop

  
//Drag and Drop script - http://www.btinternet.com/~kurt.grigg/javascript

/*
1: Put this style sheet and the dragdrop.js link between the head tags of your page html.

   IMPORTANT! Do not remove "position : relative;" from the dragclass style.

<style type="text/css">
.dragclass{
position : relative;
cursor : move;
}
</style>

<script type="text/javascript" src="dragdrop.js"></script>



2: To apply drag and drop status to something just give it the dragclass.

 Example:

 <p class="dragclass" style="color:red">
 Blah blah 
 </p>

*/

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

<title>Drag and Drop</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="content-script-type" content="text/javascript">
<meta http-equiv="content-style-type" content="text/css">


<style type="text/css">
.dragclass{
position : relative;
cursor : move;
}
</style>


<script type="text/javascript">
//Drag and Drop script - http://www.btinternet.com/~kurt.grigg/javascript

if  (document.getElementById){

(function(){

//Stop Opera selecting anything whilst dragging.
if (window.opera){
document.write("<input type='hidden' id='Q' value=' '>");
}

var n = 500;
var dragok = false;
var y,x,d,dy,dx;

function move(e){
if (!e) e = window.event;
 if (dragok){
  d.style.left = dx + e.clientX - x + "px";
  d.style.top  = dy + e.clientY - y + "px";
  return false;
 }
}

function down(e){
if (!e) e = window.event;
var temp = (typeof e.target != "undefined")?e.target:e.srcElement;
if (temp.tagName != "HTML"|"BODY" && temp.className != "dragclass"){
 temp = (typeof temp.parentNode != "undefined")?temp.parentNode:temp.parentElement;
 }
if (temp.className == "dragclass"){
 if (window.opera){
  document.getElementById("Q").focus();
 }
 dragok = true;
 temp.style.zIndex = n++;
 d = temp;
 dx = parseInt(temp.style.left+0);
 dy = parseInt(temp.style.top+0);
 x = e.clientX;
 y = e.clientY;
 document.onmousemove = move;
 return false;
 }
}

function up(){
dragok = false;
document.onmousemove = null;
}

document.onmousedown = down;
document.onmouseup = up;

})();
}//End.
</script>


</head>
<body>






<p class="dragclass" style="top:0px;left:0px;width:200px;text-align:center;background-color:#ff0000;color:#ffffff">
P tag 
</p>


<div class="dragclass" style="height:20px;width:150px;top:0px;left:0px;background-color:#ff0000;color:#ffffff">
Div: Relative position
</div>

<p>.</p>



<img src="http://www.java2s.com/style/logo.png" class="dragclass" style="top:0px;left:0px;height:100px;width:150px;padding:0px"/>
<p>.<p>


<b class="dragclass" style="top:0px;left:0px;background-color:#ff0000;color:#ffffff">
B tag
</b>



<img src="http://www.java2s.com/style/logoRed.png" class="dragclass" style="position:absolute;top:400px;left:200px;height:105px;width:150px;padding:0px"/>

<div id="test" class="dragclass" style="position:absolute;top:330px;left:160px;height:20px;width:150px;background-color:#ff0000;color:#ffffff">
Div: Absolute position
</div>


</body>
</html>

           
         
    
  








Related examples in the same category

1.Catches and manages the mouse's events
2.Mouse and key event (IE)
3.Mouse in image and out
4.Image Mouse on and out
5.Mouse cross hairs
6. Creating a Rollover Effect
7.Codependent Link Tag and the onMouseOver Event
8.Which mouse button was clicked?
9.Which element was clicked
10.Mouse over event
11.Get component From Point (Mouse)
12.Cutting and Pasting under Script Control
13.Using Drag-Related Event Handlers
14. Using onDragEnter and onDragLeave Event Handlers
15.Using onMouseDown and onMouseUp Event Handlers
16.Dragging Elements with onMouseMove
17.Using the toElement and fromElement Properties
18. The onBeforeCopy Event Handler
19.Called from an onmousedown event handler
20.Mousedown event handler of an object within a Layer
21.Cursor Arrival and Departure
22.Use Mouse over action to transfer url location
23.Get mouse position with on mouse move event (IE)
24.Get mouse position in mouse down event (IE)
25.H1 double click events
26.Return boolean value for on click event
27.Register mouse down event(IE)