I would like to track user’s mouse and display a gradient text using background-clip: text;
The text should always be visible with a color of black. Only when hovering the text, the gradient should show up (inside of text) and move together with mouse movement.
Is something like this possible?
I found this tutorial where they’re animating the background. However when I am trying to add background-clip: text;
it doesn’t work anymore.
Here is what I got:
const button = document.querySelector(".shiny");
button.addEventListener("mousemove", (e) => {
const { x, y } = button.getBoundingClientRect();
button.style.setProperty("--x", e.clientX - x);
button.style.setProperty("--y", e.clientY - y);
});
.shiny {
position: relative;
display: block;
color: black;
padding: 10px 15px;
background: #3984ff;
border-radius: 10px;
font-weight: bold;
font-size: 33px;
overflow: hidden;
}
.shiny::after {
content: "";
position: absolute;
top: calc(var(--y, 0) * 1px - 50px);
left: calc(var(--x, 0) * 1px - 50px);
width: 100px;
height: 100px;
opacity: 0;
transition: opacity 0.4s;
}
.shiny:hover::after {
opacity: 0.9;
background: radial-gradient(black, red 80%);
*-webkit-background-clip: text;
*background-clip: text;
*-webkit-text-fill-color: transparent;
}
<a class="shiny" href="">BIG AND BOLD TEXT</a>