1<!doctype html>
2<html lang="en">
3<head>
4 <meta charset="utf-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1">
6 <title>jQuery UI Draggable - Default functionality</title>
7 <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
8 <link rel="stylesheet" href="/resources/demos/style.css">
9 <style>
10 #draggable { width: 150px; height: 150px; padding: 0.5em; }
11 </style>
12 <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
13 <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
14 <script>
15 $( function() {
16 $( "#draggable" ).draggable();
17 } );
18 </script>
19</head>
20<body>
21
22<div id="draggable" class="ui-widget-content">
23 <p>Drag me around</p>
24</div>
25
26
27</body>
28</html>
29
1<script>
2 var dragged;
3
4 /* events fired on the draggable target */
5 document.addEventListener("drag", function(event) {
6
7 }, false);
8
9 document.addEventListener("dragstart", function(event) {
10 // store a ref. on the dragged elem
11 dragged = event.target;
12 // make it half transparent
13 event.target.style.opacity = .5;
14 }, false);
15
16 document.addEventListener("dragend", function(event) {
17 // reset the transparency
18 event.target.style.opacity = "";
19 }, false);
20
21 /* events fired on the drop targets */
22 document.addEventListener("dragover", function(event) {
23 // prevent default to allow drop
24 event.preventDefault();
25 }, false);
26
27 document.addEventListener("dragenter", function(event) {
28 // highlight potential drop target when the draggable element enters it
29 if (event.target.className == "dropzone") {
30 event.target.style.background = "purple";
31 }
32
33 }, false);
34
35 document.addEventListener("dragleave", function(event) {
36 // reset background of potential drop target when the draggable element leaves it
37 if (event.target.className == "dropzone") {
38 event.target.style.background = "";
39 }
40
41 }, false);
42
43 document.addEventListener("drop", function(event) {
44 // prevent default action (open as link for some elements)
45 event.preventDefault();
46 // move dragged elem to the selected drop target
47 if (event.target.className == "dropzone") {
48 event.target.style.background = "";
49 dragged.parentNode.removeChild( dragged );
50 event.target.appendChild( dragged );
51 }
52 }, false);
53
54</script>