Minion Eyes Follow Mouse Cursor using JavaScript
Hello, today we’re going to learn how to use HTML, CSS & JavaScript to create a Minion Eyes Follow Mouse Cursor. By following these instructions, you can simply make this Minion Eyes Follow Mouse Cursor in HTML, CSS & JavaScript. Simply by adhering to the procedures mentioned below, you will be able to develop this amazing Minion Eyes Follow Mouse Cursor.
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 Minion Eyes Follow 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 Minion Eyes Follow Mouse Cursor Project.
Step 3
At last we will use JS (JavaScript) which will add a logic to make the Minion Eyes Follow Mouse Cursor Project functioning from the user end.
I hope you have got an idea about the project.
HTML Code for Minion Eyes Follow Mouse Cursor
Portfolio Website using HTML and CSS (Source Code)
<html lang="en"> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Minion Eyes Follow Mouse</title> <!-- Stylesheet --> <link rel="stylesheet" href="style.css" /> </head> <body> <div class="container"> <div class="eyes-wrapper"> <div class="eye"> <div class="eyeball"></div> </div> <div class="eye"> <div class="eyeball"></div> </div> </div> </div> <!-- Script --> <script src="script.js"></script> </body> </html>
First we’ll start with creating the structure of the Minion Eyes Follow 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 Minion Eyes Follow Mouse Cursor
body { padding: 0; margin: 0; background-color: #f5d60e; } .container { width: 100vw; height: 100vh; display: flex; align-items: center; justify-content: center; } .container:before { content: ""; position: absolute; width: 100%; height: 4em; background-color: #231f1e; transform: translate(-50%, -50%); top: 50%; left: 50%; z-index: -1; } .eyes-wrapper { display: flex; } .eyes-wrapper:before { content: ""; position: absolute; width: 26em; height: 6em; background-color: #a8a7ac; margin: auto; left: 0; right: 0; top: 0; bottom: 0; z-index: -1; } .eye { width: 10em; height: 10em; border: 15px solid #a6a4ad; background-color: #ffffff; border-radius: 50%; } .eyeball { height: 3.2em; width: 3.2em; background: radial-gradient(#271e1e 35%, #935a29 37%); border-radius: 50%; margin: 0.2em 3.5em; position: relative; } .eyeball:before { content: ""; position: absolute; background-color: #ffffff; height: 0.7em; width: 0.5em; border-radius: 50%; top: 13px; left: 13px; transform: rotate(45deg); }
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 Minion Eyes Follow Mouse Cursor 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 Minion Eyes Follow Mouse Cursor
//Selecting the eye div let eye_ref = document.querySelectorAll(".eye"); //mousemove for devices with mouse aand touchmove for touchcreen devices let events = ["mousemove", "touchmove"]; //Check for touch screen function isTouchDevice() { try { document.createEvent("TouchEvent"); return true; } catch (e) { return false; } } //Same function for both events events.forEach((eventType) => { document.body.addEventListener(eventType, (event) => { eye_ref.forEach((eye) => { /* getBoundingClientRect() method returns the position relative to the viewport */ let eyeX = eye.getBoundingClientRect().left + eye.clientWidth / 2; let eyeY = eye.getBoundingClientRect().top + eye.clientHeight / 2; /* ClientX and ClientY return the position of clients cursor from top left of the screen*/ var x = !isTouchDevice() ? event.clientX : event.touches[0].clientX; var y = !isTouchDevice() ? event.clientY : event.touches[0].clientY; /* Subtract x position of mouse from x position of eye and y position of mouse from y position of eye. Use atan2(returns angle in radians) */ let radian = Math.atan2(x - eyeX, y - eyeY); //Convert Radians to Degrees let rotationDegrees = radian * (180 / Math.PI) * -1 + 180; //Rotate the eye eye.style.transform = "rotate(" + rotationDegrees + "deg)"; }); }); });
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 script we have defined the eyes of minion and pass on some functions so that it follows the cursor on the user action and make the project functioning.
100+ JavaScript Projects With Source Code ( Beginners to Advanced)
Let us see the Final Output of the project Minion Eyes Follow Mouse Cursor using HTML, CSS & JavaScript.
Output
Live Preview of Minion Eyes Follow Mouse Cursor
See the Pen
Minion Eyes by Harsh Sawant (@harshh9)
on CodePen.
We have Successfully created our Minion Eyes Follow Mouse Cursor using HTML, CSS & JavaScript (Source Code). 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