-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
82 lines (74 loc) · 2.23 KB
/
test.html
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
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
grid-template-columns: 1fr 5fr;
grid-gap: 10px;
width: 100vw;
height: 100vh;
}
.grid-item {
background-color: lightblue;
resize: both; /* Enables resizing for the element */
overflow: auto; /* Enables scrolling if content overflows when resizing */
position: relative; /* Needed for the absolute-positioned drag trigger */
}
.drag-trigger {
width: 100%;
height: 30px;
background-color: rgba(0, 0, 0, 0.3);
cursor: move;
position: absolute;
top: 0;
left: 0;
}
</style>
</head>
<body>
<div class="grid-container">
<div class="grid-item" id="resizable-element1">
<div
class="drag-trigger"
draggable="true"
ondragstart="drag(event, 'resizable-element1')"
></div>
<p>Resizable Element 1</p>
</div>
<div class="grid-item" id="resizable-element2">
<div
class="drag-trigger"
draggable="true"
ondragstart="drag(event, 'resizable-element2')"
></div>
<p>Resizable Element 2</p>
</div>
</div>
<script>
function drag(event, elementId) {
event.dataTransfer.setData('text/plain', elementId);
}
document.addEventListener('dragover', function (event) {
event.preventDefault();
});
document.addEventListener('drop', function (event) {
event.preventDefault();
const data = event.dataTransfer.getData('text');
const targetElement = event.target.closest('.grid-item');
if (targetElement) {
const gridContainer = document.querySelector('.grid-container');
const elements = Array.from(gridContainer.children);
const targetIndex = elements.indexOf(targetElement);
const sourceIndex = elements.findIndex(
(element) => element.id === data
);
if (targetIndex !== sourceIndex) {
// Swap elements in the grid
gridContainer.insertBefore(elements[sourceIndex], targetElement);
}
}
});
</script>
</body>
</html>