I'm trying to create a button with an interesting hover effect that moves along with the mouse cursor.
However, I'm facing difficulties in making the button move according to the mouse movements.
<div class="main">
<div class="button">
Explore the entire collection
</div>
</div>
.main {
width:100vw; height:100vh;
display:flex;
align-items:center;
justify-content:center;
}
.button {
padding:32px;
background-color:green;
color:white;
position:relative;
&:after {
content:'';
display: block;
height:100%;
width:100%;
position:absolute;
left:10px;
top:10px;
background-color:yellow;
}
}
const btn = document.querySelector('.button');
const btnAfterStyle = (window.getComputedStyle(btn, ':after'));
console.log(btnAfterStyle.getPropertyValue('top'))
btn.addEventListener("mousemove", function( e ) {
console.log(e.clientX);
btnAfterStyle.getPropertyValue('top') == `${e.clientY-20}px`;
btnAfterStyle.getPropertyValue('left') == `${e.clientX-75}px`;
});
There are no errors in my code, but the properties are not updating as expected.
Any suggestions or solutions? Thank you!