Instead of placing the desired element in the background, consider positioning it in front and revealing only a portion using a clip-path;
When specifying coordinates, you can utilize CSS variables. Alternatively, you have the option to directly modify the style.
// Access DOM element
const container = document.querySelector('.container');
// Implement event listener
container.addEventListener('mousemove', updateCoords, false);
function updateCoords(event) {
// Capture X and Y coordinates
const { offsetX, offsetY } = event;
// Update coordinates
container.style.setProperty('--x', offsetX + 'px');
container.style.setProperty('--y', offsetY + 'px');
}
.container {
border: 1px solid #000;
width: 300px;
height: 300px;
}
/* Display child element upon hovering over the container */
.container:hover .child {
display: block;
}
.child {
clip-path: ellipse(30px 30px at var(--x) var(--y));
display: none;
}
To achieve smoother movement of the circle, consider utilizing requestAnimationFrame.
// Obtain DOM element
const container = document.querySelector('.container');
// Implement event listener
container.addEventListener('mousemove', updateCoords, false);
function updateCoords(event) {
// Capture X and Y coordinates
const { offsetX, offsetY } = event;
// Update coordinates
requestAnimationFrame(() => {
container.style.setProperty('--x', offsetX + 'px');
container.style.setProperty('--y', offsetY + 'px');
});
}
.container {
border: 1px solid #000;
width: 300px;
height: 300px;
}
/* Show child element when container is hovered over */
.container:hover .child {
display: block;
}
.child {
clip-path: ellipse(30px 30px at var(--x) var(--y));
display: none;
}
An example featuring text:
// Access DOM element
const container = document.querySelector('.container');
// Implement event listener
container.addEventListener('mousemove', updateCoords, false);
function updateCoords(event) {
// Capture X and Y coordinates
const {offsetX, offsetY} = event;
// Update coordinates
requestAnimationFrame(() => {
container.style.setProperty('--x', offsetX + 'px');
container.style.setProperty('--y', offsetY + 'px');
});
}
.container {
min-height: 100vh;
min-width: 100vh;
overflow: hidden;
}
.container:hover .code {
display: flex;
}
.display,
.code {
position: absolute;
display: flex;
align-items: center;
justify-content: center;
width: 100vw;
height: 100vh;
background-color: rgb(49, 49, 49);
color: rgb(240, 191, 29);
pointer-events: none;
}
.code {
clip-path: ellipse(100px 100px at var(--x) var(--y));
display: none;
background-color: rgb(3, 3, 3);
color: rgb(101, 253, 101);
}
This showcases how to reveal content with a virtual circle effect: