DEV Community

Codewithrandom Blogs
Codewithrandom Blogs

Posted on

Div Follows Mouse Cursor using HTML & JavaScript

Hello, today we're going to learn how to use HTML, CSS & JavaScript to create a Div Follows Mouse Cursor. 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>
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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);
});
Enter fullscreen mode Exit fullscreen mode

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 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

Top comments (2)

Collapse
 
growkon21 profile image
Growkon

Thank you for this thoughtful post! I really appreciated the section where you discussed it resonated with me because I’ve been exploring similar ideas in my own life.

I also liked how you highlighted key points, as it’s something that often gets overlooked but makes such a big difference.

Looking forward to reading more of your insights. Keep up the great work!
IB Schools in Hyderabad

Collapse
 
sjanhsu profile image
Anima Jana

This is such an insightful post! I really appreciate how you broke down the topic in a way that's easy to understand. Looking forward to implementing some of these tips in my daily routine!
International schools in Hyderabad