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. Dojo toolkit
7. Event
8. Event onMethod
9. Ext JS
10. Form Control
11. GUI Components
12. HTML
13. Javascript Collections
14. Javascript Objects
15. Javascript Properties
16. jQuery
17. Language Basics
18. Mochkit
19. Mootools
20. Node Operation
21. Object Oriented
22. Page Components
23. Rico
24. Scriptaculous
25. Security
26. SmartClient
27. Style Layout
28. Table
29. Utilities
30. Window Browser
31. YUI Library
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 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.java2java.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.java2java.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)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.