Drag-n-drop in HTML, CSS & JavaScript

Learn how to add Drag-n-drop functionality to your website using HTML, CSS, and JavaScript to create a more interactive user experience.
drag-n-drop

Table of Contents

Drag and drop functionality is a popular user interaction that allows users to move and rearrange items on a website by dragging them with their mouse and dropping them to a new location. This functionality is widely used in various web applications, such as file upload, list sorting, and image cropping. In this article, we will guide you on how to implement drag and drop functionality in your website using HTML, CSS, and JavaScript.

Step 1: Create the HTML Markup

The first step is to create the HTML markup for the items you want to drag and drop. You can use the div tag with a unique class or ID to represent each item. You can also add content or images inside each item div.

				
					<!DOCTYPE html>
<!-- By Mr. Geek - www.wphotshot.com -->
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Captcha Generator | WPHotshot</title>
  <link rel="stylesheet" href="style.css" />
  <script src="main.js" defer></script>
  <!-- Fontawesome CDN Link -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css" />
</head>

<body>
  <section class="container">
    <div class="box">
      <div class="image" draggable="true"></div>
    </div>
  </section>
</body>

</html>
				
			

Step 2: Add CSS Styling

Next, you can add CSS styling to the items to make them visually appealing and distinguishable. You can customize the appearance of the items, such as their size, position, color, and border. You can also use CSS animations and transitions to create smooth and engaging drag and drop effects.

				
					/* Import Google font - Poppins */
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700&display=swap");

@import url('https://fonts.googleapis.com/css2?family=DM+Serif+Display:ital@0;1&display=swap');

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
}

body {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(to right, #2832d4, #00A5B2, #7209D4);
}
.container {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  width: 1000px;
  justify-content: center;
  background-color: #f8f8f8;
  border-radius: 15px;
}
.container .box {
  position: relative;
  height: 350px;
  width: 1600px;
  border-radius: 12px;
  border: 2px solid #333;
}
.box .image {
  height: 100%;
  width: 100%;
  border-radius: 10px;
  background-size: cover;
  background-position: center;
  background-image: url("img.jpg");
}
.box.hovered {
  border: 2px dashed #333;
}
				
			

Step 3: Implement JavaScript Drag-n-drop Logic

The most crucial step in implementing drag and drop functionality is the JavaScript logic. You can use JavaScript to create a function that allows users to drag and drop the items. You can do this by adding event listeners to the items and using JavaScript’s built-in drag and drop methods, such as dragstart, dragenter, dragover, drop, and dragend, to handle the drag and drop events.

				
					//DOM Elements
const boxes = document.querySelectorAll(".box"),
  image = document.querySelector(".image");

//Loop through each boxes element
boxes.forEach((box) => {
  //When a draggable element dragged over a box element
  box.addEventListener("dragover", (e) => {
    e.preventDefault(); //Prevent default behaviour
    box.classList.add("hovered");
  });

  //When a draggable element leaves box element
  box.addEventListener("dragleave", () => {
    box.classList.remove("hovered");
  });

  //When a draggable element is dropped on a box elemen
  box.addEventListener("drop", () => {
    box.appendChild(image);
    box.classList.remove("hovered");
  });
});
				
			

Step 4: Test and Optimize

After implementing the drag and drop functionality, it’s essential to test it thoroughly to ensure that it works as intended. You can test the functionality on different devices and browsers to check for any compatibility issues. You can also optimize the functionality by adding additional features, such as feedback and restrictions, to improve the user experience.

Conclusion

In conclusion, adding drag and drop functionality to your website can create a more interactive and engaging user experience. By following the steps above, you can implement drag and drop functionality using HTML, CSS, and JavaScript that will allow users to move and rearrange items on your website effortlessly. Remember to test your code thoroughly and optimize the functionality to ensure that it meets the needs and preferences of your users. Implementing drag and drop functionality on your website can improve user engagement and ultimately lead to higher conversion rates.

Find more tutorials regarding Web Development here.

Leave a Reply

Your email address will not be published. Required fields are marked *