Div Follows Mouse Cursor using HTML & JavaScript

Div Follows Mouse Cursor using HTML & JavaScript

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.

follows the mouse cursor in HTML and JavaScript is a dynamic and engaging feature for web development. By utilizing the mousemove event, you can effortlessly track the cursor’s position and update the div’s location accordingly. The example code ensures smooth and precise movement of the div, enhancing the user experience on the webpage. Customization options for the div’s size, color, and shape provide flexibility for seamless integration into diverse web designs. Overall, this interactive element adds a touch of interactivity, making websites more visually appealing.

In this example:

  • The mousemove event is used to detect when the mouse is moved.
  • The follower div is the element that will follow the mouse cursor.
  • The left and top CSS properties of the follower div are updated based on the mouse position, ensuring that the div is centered around the cursor.
  • pointer-events: none; is used to ensure that the follower div doesn’t interfere with mouse interactions.
 
 

follow mouse cursor javascript

Project Description

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

100+ JavaScript Projects With Source Code ( Beginners to Advanced)

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

Ecommerce Website Using HTML, CSS, & JavaScript (Source Code)

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

Output:- 

Live Preview of Div Follows Mouse Cursor

https://codepen.io/harshh9/pen/YzvjgKG

See the Pen
Mouse Cursor
by Harsh Sawant (@harshh9)
on CodePen.

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

FAQs:-

How to set radius in CSS file?

To set radius in CSS file we have to use this code:-
border-radius: 50%;

How to attach the HTML file of this “Div Follow Mouse Cursor” program with the JavaScript file?

You can use these lines of code to attach the HTML file of this div follow mouse cursor program with the JavaScript file:-
<script src="script.js"></script>
I used script.js in the example, you can use the file name of your java script.



Leave a Reply