I am attempting to create a unique grid layout with 3x3 dimensions, where each grid item represents a fragment of a single image. These items should have the capability to be dragged and dropped inside another 3x3 grid, in any desired sequence.
I have hit a obstacle while working on this task, and I have been trying to resolve it for quite some time now.
Directory structure:
.
├── Project/
│ └── Res/
│ ├── Media/
│ │ └── Images/
│ │ └── image.jpg
│ ├── Scripts/
│ │ └── anim3.js
│ ├── Styles/
│ │ └── anim3.css
│ └── Pages/
│ └── animPage3.html
└── index.html(not relevant)
animPage3.html:
<body>
<head>
<link
rel="stylesheet" href="Res/Styles/anim3.css">
</head>
<div class="draggable-grid">
<div class="box" draggable="true"></div>
...There's a total of 9 these...
</div>
<div class="droppable-grid">
<div class="droppable-box"></div>
<!--There's a total of 9 of these "droppable-box" divs-->
</div>
<script src="Res/Scripts/anim3.js">
</body>
anim3.css:
.grid-cont {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 10px;
}
.draggable-grid, .droppable-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
gap: 10px;
}
.box {
width: 100px;
height: 100px;
background-image: url('../Media/Images/image.jpg');
background-size: 300% 300%;
}
.droppable-box {
border: 2px dashed #ccc;
height: 100px;
}
anim3.js:
const draggableBoxes = document.querySelectorAll('.draggable-grid .box');
const droppableBoxes = document.querySelectorAll('.droppable-grid .droppable-box');
draggableBoxes.forEach(box => {
box.addEventListener('dragstart', dragStart);
});
droppableBoxes.forEach(droppableBox => {
droppableBox.addEventListener('dragover', dragOver);
droppableBox.addEventListener('dragenter', dragEnter);
droppableBox.addEventListener('dragleave', dragLeave);
droppableBox.addEventListener('drop', drop);
});
let draggedItem = null;
function dragStart() {
draggedItem = this;
setTimeout(() => this.style.display = 'none', 0);
}
function dragOver(e) {
e.preventDefault();
}
function dragEnter(e) {
e.preventDefault();
this.classList.add('hovered');
}
function dragLeave() {
this.classList.remove('hovered');
}
function drop() {
this.classList.remove('hovered');
this.append(draggedItem);
draggedItem.style.display = 'block';
draggedItem = null;
}
I attempted to comment out the following line:
//setTimeout(() => this.style.display = 'none', 0);
However, it did not fix the issue with dragging functionality.
Desired outcome of the code: Create two 3x3 grids. The first grid contains draggable items, where each item represents a portion of a single image. The second grid serves as the drop zone for the items, allowing placement in any of the 9 boxes within the grid.