ADD THIS EFFECT TO YOUR WEBSITE !! 🤯
Tags: webdev, beginners, programming, tutorial
Published: 2024-06-03 10:53:46

Today I will tell you in detail how I made this:

{% codepen https://codepen.io/Gitguy2328_xprogram/pen/XWwRWOP %}

So as you can see. I will be telling you how to make a mouse trailer

<img src='https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExcGlpMnY4M3NrMGRkYjFkbnRoYXkwd3A5MDB6ZXc2bmhtODMyYzQ1diZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/oobNzX5ICcRZC/giphy.gif'/>

## Basic setup

It is pretty simple to make the navbar, generally we don't see navbars with backgrounds. They generally have transparent background. But I got inspiration from one of the websites I recently saw and I needed to remake it. I started off by having a good looking sizing. Cause it should look perfect not lacky. Then I gave it vibrant colours that pop. Dodgerblue and yellow was a good looking pair. Next I got the icons from font awesome. aligned them gave them colour and a hover effect. I thought it would be cool if I threw a bunch of other effects on like the background changes when we hover on something. This was the basic setup
<img src='https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExZjR0b3BhNWRxaGY2aGY1emFoYWY3ZGdjdDN3Zm5meG5kbHh5ZWpkMyZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/5WAdRevloGjuw/giphy.gif'/>

##Mouse

First of all we need a pretty basic trailer to make it follow the mouse. I started off by making a 100 px wide square and then rounded it's edges to make a circle. Then I got the glassmorphism code from css.glass. After that I put Google icon inside that trailer. I dont know why. Then I centered it.

<img src='https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExd3l1d3d5ZzJ0MnpscGJjZjdwYjI0bGJpODUzOTAwMHdsY2FvaXY5diZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/eK12uCsrAh4wmTXejp/giphy.gif'/>

> Tip: I always center things with one basic code: 

```css
display: flex;
align-items: center;
justify-content: center;

/* If you want them to be on top of each other */

flex-direction: column;
```

> I just prefer that code for some reason

Then I set the position of the trailer fixed, so that it is fixed 🙄. I set the z-index to something big. Like 999999999999999999999999999999999.
<img src='https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExMXRsaXUxeXgycTlzNmRxZGU4Z205aW5ienNmdHdpaXlycGJqMGdwMiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/coyiAVHZSyGqs/giphy.gif'/>
Another important thing is to set the top and left to 0. This will get our mouse which will be following the mouse ready.

## Make it follow 🚶‍♂️

Just enter this code

```js
var trailer = document.getElementById("trailer");
window.onmousemove = (e) => {
  document.getElementById("trailer").style.scale = "1";
  const y = e.clientX + 20;
  const x = e.clientY + 20;

  const keyframes = {
    transform: "translate(" + y + "px" + "," + x + "px)"
  };

  document.getElementById("trailer").animate(keyframes, {
    duration: 800,
    fill: "forwards"
  });
};
```
<img src='https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExaTN6NnE4d2UxMWZsODFsamFlbzFsYW03cHJoMnhxd2V6YmdsNHZnOCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/Dh5q0sShxgp13DwrvG/giphy.gif'/>

Now what this code does basically is monitors the mouse's movement and then stores it. We then add 20 to the value and set it as the x and y position of the trailer in the form of an animation. We can do it basically but it's not so smooth and soothing. If you want it to be smooth. You will have to use animations. That's all if you followed my instructions you should get something to follow your mouse.

<img src='https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExOW9hcXZ3NWg0ODE1b254ajQ5ajF3b2s5ZHBjc2lzMjh6ODdzcTk0dCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/l0Iyl55kTeh71nTXy/giphy.gif'/>

Guys now I want to ask you something what I should I tell you about next !