DAY 4 PROJECT : DRAG & DROP
Tags: webdev, javascript, beginners, programming
Published: 2024-06-27 19:51:08

**PROJECT NAME : Creating a Fun Drag and Drop Color Game Using HTML, CSS, and JavaScript**

The main objective of this project is to create a game where users can drag colored boxes and drop them into designated areas. The game will include multiple colored boxes, and users can enjoy the interaction and visual appeal of dragging and dropping these elements.
This project is a fantastic way to learn and practice DOM manipulation, event handling, and styling in web development. Let's dive into the details of this fun and educational project!

**Technologies Used**

**HTML**: For structuring the web page.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l43ow8by709viik3f465.png)

**CSS**: For styling the game elements.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/huwa307qrovybme0n0of.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9wx1lofmq72l4aca1s2a.png)

**JavaScript**: For adding interactivity and handling the drag-and-drop functionality.

The drag function enables drag-and-drop functionality for elements with the draggable attribute. It achieves this by handling mouse events (mousedown, mousemove, and mouseup) to update the position of the elements as they are dragged.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c7los5lo4z41w45d2kmf.png)

**1) INITIAL SETUP**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mbliizcqfe8s1jh1a18h.png)

- dragging: A flag to track the element currently being dragged.
- mouseX and mouseY: Store the mouse's initial x and y coordinates when the drag starts.
- eleX and eleY: Store the initial x and y coordinates of the element being dragged.
- boxes: Select all elements with the draggable attribute.
- boxes.forEach: Iterate over each draggable element and attach a mousedown event listener to initiate the drag. Also, initialize the top and left CSS properties of each element to 0.

**2) STARTING THE DRAG**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rkbnfvk15r939veceuqv.png)

- e.preventDefault(): Prevent default behavior to ensure smooth dragging.
- dragging = this: Set the dragging variable to the element that triggered the mousedown event.
- mouseX and mouseY: Capture the initial mouse position.
- eleX and eleY: Capture the initial position of the element being dragged.
- Attach mousemove and mouseup event listeners to the document to handle the drag and drop actions.

**3)HANDLING THE DRAG MOVEMENT**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/izaffzq7a7jy2anu9ty9.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nb7cw62081mkl78dgj12.png)

- deltaMouseX and deltaMouseY: Calculate the change in mouse position since the drag started.
- Update the left and top styles of the dragging element based on the change in mouse position and the element's initial position.

**4)ENDING THE DRAG**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9pizy0ju2oeje37t3ubq.png)

- Set dragging to false to indicate that no element is being dragged anymore.
- Optionally, you could remove the mousemove and mouseup event listeners here to clean up, although not strictly necessary.

**5)INITIALIZE THE DRAG FUNCTION**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i8wxwigixwts83fkzmtr.png)

All the drag function to set up the drag-and-drop functionality for all draggable elements.

Creating a drag and drop game using HTML, CSS, and JavaScript is an exciting way to improve your web development skills. This project covers the essentials of DOM manipulation and event handling, providing a solid foundation for more advanced projects. Give it a try, and see how creative you can get with your own variations!

THANK YOU!
HAPPY CODING!