How to make a follow cursor effect with JavaScript
Follow cursor effect using Javascript, GSAP and CSS
Dec 29, 2020 | Read time 4 minutes🔔 Table of contents
See the demo here
I am seeing a recent trend of portfolio sites having a following-cursor effect. That is - circle that follows where your mouse moves - but it is a bit delayed.
For my #100DaysOfCode challenge, I decided to implement this using GSAP. You could probably do this with regular javascript and CSS keyframes, but GSAP makes things a lot quicker/ simpler.
HTML
We start off with the following basic HTML.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Change Cursor On Hover</title>
<link rel="stylesheet" href="style.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/latest/TweenMax.min.js"></script>
</head>
<body>
<div id="wrapper">
<div class="image-item">
<div class="image-thumb">
<img src="1.jpg" alt="" />
</div>
</div>
<div class="image-item">
<div class="image-thumb">
<img src="2.jpg" alt="" />
</div>
</div>
<div class="image-item">
<div class="image-thumb">
<img src="3.jpg" alt="" />
</div>
</div>
<div class="image-item">
<div class="image-thumb">
<img src="4.png" alt="" />
</div>
</div>
</div>
<div class="cursor"></div>
<div class="cursor-follower"></div>
<script src="main.js"></script>
</body>
</html>
CSS
html,
body {
margin: 0;
padding: 0;
cursor: none;
margin-top:50px;
background: #161616;
}
* {
box-sizing: border-box;
}
#wrapper {
width: 100%;
background: #161616;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
.cursor {
position: absolute;
background: #dee2e6;
width: 8px;
height: 8px;
border-radius: 100%;
z-index: 1;
transition: 0.5s cubic-bezier(0.75, -1.27, 0.3, 2.33) transform,
0.2s cubic-bezier(0.75, -1.27, 0.3, 2.33) opacity;
user-select: none;
pointer-events: none;
transform: scale(0.8);
}
.cursor::before {
content: "";
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
display: block;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
border-radius: 100%;
opacity: 0;
}
.cursor.active {
opacity: 1;
transform: scale(7);
background-color: yellow;
}
.cursor.active::before {
content: "";
opacity: 1;
}
.cursor-follower {
position: absolute;
background: rgba(255, 255, 255, 0.1);
width: 40px;
height: 40px;
border-radius: 100%;
z-index: 1;
transition: 0.6s cubic-bezier(0.75, -1.27, 0.3, 2.33) transform,
0.4s cubic-bezier(0.75, -1.27, 0.3, 2.33) opacity;
user-select: none;
pointer-events: none;
transform: translate(4px, 4px);
}
.cursor-follower.active {
opacity: 0.3;
transform: scale(0);
}
.image-thumb {
overflow: hidden;
transition: all 0.5s cubic-bezier(0.25, 1, 0.3, 1);
}
.image-thumb img {
max-width: 660px;
opacity: 0.4;
transition: all 0.5s cubic-bezier(0.25, 1, 0.3, 1);
transform-origin: 90% center;
}
.image-item:hover .image-thumb {
transform: translateX(-1.75rem);
}
.image-item:hover .image-thumb img {
opacity: 0.8;
transform: scale(1.2);
}
.image-item{
padding-bottom: 2rem;
}
Javascript / GSAP
So here is the Javascript/GSAP code. Theres two things thats happening here:
(1) To get the ‘delayed’ follow effect, we use the setInterval function every 10 milliseconds. Inside this function we set the ‘cursor’ and ‘cursor-follower’ element positions using the GSAP ‘set’ function.
(2) When the mouse hovers over each of the images, we apply an ‘active’ class to the ‘cursor’ and the ‘cursor-follower’ element. This just CSS that scales up the cursor element and changes the colour to yellow.
let cursor = document.getElementsByClassName("cursor")[0];
let follower = document.getElementsByClassName("cursor-follower")[0];
const body = document.getElementById('main');
let imgs = document.querySelectorAll(".image-item img");
let posX = 0,
posY = 0,
mouseX = 0,
mouseY = 0;
/* Update the cursor and its follower's position every 10 miliseconds*/
setInterval(function () {
posX += (mouseX - posX) / 9;
posY += (mouseY - posY) / 9;
TweenMax.set(follower, {
css: {
left: posX - 20,
top: posY - 20
}
});
TweenMax.set(cursor, {
css: {
left: mouseX,
top: mouseY
}
});
}, 10);
/* Track the x and y position*/
body.addEventListener('mousemove', e => {
mouseX = e.pageX;
mouseY = e.pageY;
});
/* Apply the active class when the cursor hovers over the thumbnail images*/
imgs.forEach(function(elem, index){
elem.addEventListener('mouseenter', e => {
cursor.className = "cursor active";
follower.className = "cursor-follower active";
});
elem.addEventListener('mouseleave', e => {
cursor.classList.remove("active");
follower.classList.remove("active");
});
});