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

JavaScript DHTML
1. Ajax Layer
2. Data Type
3. Date Time
4. Development
5. Document
6. Event
7. Event onMethod
8. Form Control
9. GUI Components
10. HTML
11. Javascript Collections
12. Javascript Objects
13. Language Basics
14. Node Operation
15. Object Oriented
16. Page Components
17. Security
18. Style Layout
19. Table
20. Utilities
21. Window Browser
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
JavaScript DHTML » Event » Mouse Event 
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 (!ee = 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 (!ee = 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
w__w___w_._j__ava__2__s___.c__o__m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.