I have a circle that follows my curser and I also made it expand when I hover over any text. The problem is when I try to add a transition: 2s;
to the CSS to make it expand smoothly. When I add it it ruins my JavaScript animation so I was wondering if I could add only a transition time to when the the circle expands.
I also tired to add the transition: 2s;
to just the .mouseFollowCircle.big
class but it causes the same problems to the JavaScript only when I'm hovering over text
document.addEventListener('DOMContentLoaded', () => {
const interBubble = document.getElementById('circle');
let curX = 0;
let curY = 0;
let tgX = 0;
let tgY = 0;
function move() {
curX += (tgX - curX) / 10;
curY += (tgY - curY) / 10;
interBubble.style.transform = `translate(${Math.round(curX)}px, ${Math.round(curY)}px)`;
requestAnimationFrame(() => {
move();
});
}
window.addEventListener('mousemove', (e) => {
tgX = e.clientX;
tgY = e.clientY;
if (e.target.tagName === 'P' ||
e.target.tagName === 'A' ||
e.target.tagName === 'BUTTON' ||
e.target.parentNode.tagName === 'BUTTON') {
interBubble.classList.add('big');
} else {
interBubble.classList.remove('big');
}
});
move();
});
Body {
background-color: black;
overflow: hidden;
}
div {
width: 100%;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
p {
color: white;
font-size: 30px;
}
.mouseFollowCircle {
width: 50px;
height: 50px;
border: 3px solid white;
border-radius: 999px;
position: absolute;
z-index: 999;
top: -25px;
left: -25px;
box-shadow: 0 0 10px white;
pointer-events: none;
backdrop-filter: blur(2px);
}
.mouseFollowCircle.big {
width: 70px;
height: 70px;
border-radius: 999px;
border: 1px solid white;
position: absolute;
z-index: 999;
top: -35px;
left: -35px;
box-shadow: 0 0 10px white;
pointer-events: none;
backdrop-filter: blur(2px);
}
<div><p>Hello</p></div>
<section class="mouseFollowCircle" id="circle"></section>
To make the expansion effect smooth for the circle following the mouse, just adjust these properties in the .mouseFollowCircle
class:
transition: width 1s ease, height 1s ease, border 1s ease, top 1s ease, left 1s ease;
document.addEventListener('DOMContentLoaded', () => {
const interBubble = document.getElementById('circle');
let curX = 0;
let curY = 0;
let tgX = 0;
let tgY = 0;
function move() {
curX += (tgX - curX) / 10;
curY += (tgY - curY) / 10;
interBubble.style.transform = `translate(${Math.round(curX)}px, ${Math.round(curY)}px)`;
requestAnimationFrame(() => {
move();
});
}
window.addEventListener('mousemove', (e) => {
tgX = e.clientX;
tgY = e.clientY;
if (e.target.tagName === 'P' ||
e.target.tagName === 'A' ||
e.target.tagName === 'BUTTON' ||
e.target.parentNode.tagName === 'BUTTON') {
interBubble.classList.add('big');
} else {
interBubble.classList.remove('big');
}
});
move();
});
Body {
background-color: black;
overflow: hidden;
}
div {
width: 100%;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
p {
color: white;
font-size: 30px;
}
.mouseFollowCircle {
width: 50px;
height: 50px;
border: 3px solid white;
border-radius: 999px;
position: absolute;
z-index: 999;
top: -25px;
left: -25px;
box-shadow: 0 0 10px white;
pointer-events: none;
backdrop-filter: blur(2px);
transition: width 1s ease, height 1s ease, border 1s ease, top 1s ease, left 1s ease;
}
.mouseFollowCircle.big {
width: 70px;
height: 70px;
border-width: 1px;
top: -35px;
left: -35px;
}
<div><p>Hello</p></div>
<section class="mouseFollowCircle" id="circle"></section>