Div Follows Mouse Cursor using HTML &amp; JavaScript
Published: 2023-06-19 06:38:18

Hello, today we're going to learn how to use HTML, CSS & JavaScript to create a **[Div Follows Mouse Cursor](https://www.codewithrandom.com/2022/12/07/div-follows-mouse-cursor-using-javascript/)**. By following these instructions, you can simply make this Div Follows Mouse Cursor in HTML, CSS & JavaScript. Simply by adhering to the procedures mentioned below, you will be able to develop this amazing Div Follows Mouse Cursor.

**Step 1**
The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make Div Follows Mouse Cursor Project.

**Step 2**
Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Div Follows Mouse Cursor Project.

**Step 3**
At last we will use JS (JavaScript) which will add a logic to make the Div Follows Mouse Cursor Project functioning from the user end.

I hope you have got an idea about the project.

## HTML Code for Div Follows Mouse Cursor
```
<html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Div Follows Mouse Cursor</title>
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div id="my-div"></div>
    <!-- Script -->
    <script src="script.js"></script>
  </body>
</html>
```
First we'll start with creating the structure of the Div Follows Mouse Cursor project for that as you can see the above code we have used all the necessary elements & attributes to setup the structure. Let us know code the CSS part to add styling and aligned the tags.

## CSS Code for Div Follows Mouse Cursor
```
body {
  padding: 0;
  margin: 0;
  height: 100vh;
  background: linear-gradient(135deg, #8fc7f1, #7173f5);
  overflow: hidden;
}
#my-div {
  width: 6em;
  height: 6em;
  background-color: #ffffff;
  position: absolute;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  box-shadow: 0 0 20px rgba(16, 0, 54, 0.2);
  transition: 0.1s ease-out;
}
```
Second comes the CSS code, which is mentioned above in that we have styled for the structure we have padded as well as aligned the Geometric Art Generator project so that it is properly situated and doesn't get messy with suitable CSS elements. Now we have created the structure using HTML and styled the webpage using CSS its time to add the functionality using JavaScript in this project.

## JavaScript Code for Div Follows Mouse Cursor
```
let myDiv = document.getElementById("my-div");
//Detect touch device
function isTouchDevice() {
  try {
    //We try to create TouchEvent. It would fail for desktops and throw error
    document.createEvent("TouchEvent");
    return true;
  } catch (e) {
    return false;
  }
}

const move = (e) => {
  //Try, catch to avoid any errors for touch screens (Error thrown when user doesn't move his finger)
  try {
    //PageX and PageY return the position of client's cursor from top left of screen
    var x = !isTouchDevice() ? e.pageX : e.touches[0].pageX;
    var y = !isTouchDevice() ? e.pageY : e.touches[0].pageY;
  } catch (e) {}
  //set left and top of div based on mouse position
  myDiv.style.left = x - 50 + "px";
  myDiv.style.top = y - 50 + "px";
};
//For mouse
document.addEventListener("mousemove", (e) => {
  move(e);
});
//For touch
document.addEventListener("touchmove", (e) => {
  move(e);
});
```
Last stage of the project the JavaScript in which we have added the logical and coded as per the requirement with some conditions. In this code we have defined the event listener function for moving of the mouse and clicking of the mouse. Let us see the Final Output of the project Div Follows Mouse Cursor using HTML, CSS & JavaScript (Source Code).

We have Successfully created our **[Div Follows Mouse Cursor](https://www.codewithrandom.com/2022/12/07/div-follows-mouse-cursor-using-javascript/)** using HTML, CSS & JavaScript. You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to search code with random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Code Idea - codingartist

Written By – Harsh Sawant

Code By – harshh9