Day-21-Drag-N-Drop
Created with CodeSandbox
Posted on June 17, 2021
Hi Folks,
Before diving into the project, a little bit of introduction about me:
I am Sabiya Tabassum. I completed my B.Tech under CSE major. I'm currently learning React and recently, I have started 50 Projects 50 Days challenge. If you have any queries/ ideas to collaborate with me, you can connect with me at my social media handles.
Coming to our Drag N Drop Project, Let's Go!!
Glimpse of the output of our project:
From the above image, we can get an idea that there are some boxes. Out of these boxes, one box have an image. We can drag the image and drop in any of the boxes.
Tech stack we are using: HTML,CSS, JS
šHTML code:
<body>
<div class="empty">
<div class="fill" draggable="true"></div>
</div>
<div class="empty"></div>
<div class="empty"></div>
<div class="empty"></div>
<div class="empty"></div>
</body>
Detailed Explanation of HTML code:
šCSS code:
@import url("https://fonts.googleapis.com/css2?
family=Roboto:wght@400;700&display=swap");
* {
box-sizing: border-box;
}
body {
font-family: "Roboto", sans-serif;
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
margin: 0;
height: 100vh;
}
.empty {
height: 150px;
width: 150px;
margin: 10px;
border: solid 3px black;
background: white;
}
.fill {
background-image:
url("https://source.unsplash.com/random/150x150");
width: 145px;
height: 145px;
}
.hold {
border: 3px solid red;
}
.hovered {
background-color: black;
border-color: rgb(32, 230, 32);
border-style: dashed;
}
@media (max-width: 480px) {
body {
flex-direction: column;
}
}
Detailed explanation of CSS code:
šJS code:
const fill = document.querySelector(".fill");
const empties = document.querySelectorAll(".empty");
fill.addEventListener("dragstart", dragStart);
fill.addEventListener("dragend", dragEnd);
empties.forEach((empty) => {
empty.addEventListener("dragover", dragOver);
empty.addEventListener("dragenter", dragEnter);
empty.addEventListener("dragleave", dragLeave);
empty.addEventListener("drop", dragDrop);
});
function dragStart() {
this.className += " hold"; //append the hold class here
setTimeout(() => (this.className = "invisible"), 0);
}
function dragEnd() {
this.className = "fill";
}
function dragOver(e) {
e.preventDefault();
}
function dragEnter(e) {
e.preventDefault();
this.className += " hovered"; //append hovered class
here while entering into the box.
}
function dragLeave() {
this.className = "empty";
}
function dragDrop() {
this.className = "empty";
this.append(fill);
}
Detailed explanation of JS code:
Created with CodeSandbox
Hope you liked the project!!
When you're high on emotion
And you're losing your focus
And you feel too exhausted to pray
Don't get lost in the moment
Or give up when you're closest
All you need is somebody to say
It's okay not to be okay
It's okay not to be okay
When you're down and you feel ashamed
IT'S OK NOT TO BE OK!!
Posted on June 17, 2021
Sign up to receive the latest update from our blog.