-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlet selectedElement = null;.txt
155 lines (110 loc) · 4.27 KB
/
let selectedElement = null;.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
let selectedElement = null;
let x_offset = 0;
let y_offset = 0;
let isCloned = false;
// General function to make an element draggable
function makeDraggable(element) {
element.addEventListener("mousedown", onMouseDown);
}
function onMouseDown(e) {
if (this.id === "draggable-image") {
isCloned = true;
selectedElement = this.cloneNode(true);
selectedElement.style.position = "absolute";
document.body.appendChild(selectedElement);
makeDraggable(selectedElement);
} else {
isCloned = false;
selectedElement = this;
}
x_offset = e.clientX - this.getBoundingClientRect().left;
y_offset = e.clientY - this.getBoundingClientRect().top;
selectedElement.classList.add("dragging");
}
// Make the original image draggable
makeDraggable(document.getElementById("draggable-image"));
document.addEventListener("mousemove", function(e) {
if (selectedElement) {
const container = document.getElementById("container");
const containerRect = container.getBoundingClientRect();
const newX = e.clientX - x_offset;
const newY = e.clientY - y_offset;
// Check boundaries if the element is inside the container
if (selectedElement.parentElement === container) {
if (newX >= 0 && newX <= containerRect.width - selectedElement.offsetWidth) {
selectedElement.style.left = `${newX}px`;
}
if (newY >= 0 && newY <= containerRect.height - selectedElement.offsetHeight) {
selectedElement.style.top = `${newY}px`;
}
} else {
selectedElement.style.left = `${newX}px`;
selectedElement.style.top = `${newY}px`;
}
}
});
document.addEventListener("mouseup", function(e) {
if (selectedElement) {
const container = document.getElementById("container");
const containerRect = container.getBoundingClientRect();
if (isCloned &&
e.clientX > containerRect.left &&
e.clientX < containerRect.right &&
e.clientY > containerRect.top &&
e.clientY < containerRect.bottom) {
selectedElement.style.left = `${e.clientX - containerRect.left - x_offset}px`;
selectedElement.style.top = `${e.clientY - containerRect.top - y_offset}px`;
container.appendChild(selectedElement);
} else if (isCloned) {
document.body.removeChild(selectedElement);
}
selectedElement.classList.remove("dragging");
selectedElement = null;
isCloned = false;
}
});
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Draggable - Constrain movement</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<style>
.draggable { width: 90px; height: 90px; padding: 0.5em; float: left; margin: 0 10px 10px 0; }
#draggable, #draggable2 { margin-bottom:20px; }
#draggable { cursor: n-resize; }
#draggable2 { cursor: e-resize; }
#containment-wrapper { width: 95%; height:150px; border:2px solid #ccc; padding: 10px; }
h3 { clear: left; }
</style>
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
<script>
$( function() {
$( "#draggable" ).draggable({ axis: "y" });
$( "#draggable2" ).draggable({ axis: "x" });
$( "#draggable3" ).draggable({ containment: "#containment-wrapper", scroll: false });
$( "#draggable4" ).draggable({ containment: "parent" });
} );
</script>
</head>
<body>
<!-- <h3>Constrain movement along an axis:</h3>
<div id="draggable" class="draggable ui-widget-content">
<p>I can be dragged only vertically</p>
</div>
<div id="draggable2" class="draggable ui-widget-content">
<p>I can be dragged only horizontally</p>
</div>
<h3>Or to within another DOM element:</h3> -->
<div id="containment-wrapper">
<div id="draggable3" class="draggable ui-widget-content">
<p>I'm contained within the box</p>
</div>
<!-- <div class="draggable ui-widget-content">
<p id="draggable4" class="ui-widget-header">I'm contained within my parent</p>
</div> -->
</div>
</body>
</html>