JSFiddle - React, Tailwind, and code Playground
by Thalita
HTML
<div class="container">
<div class="tracker"></div>
</div>
CSS
:root {
--x: 0.5;
--y: 0.5;
}
.container, .tracker {
position: absolute;
}
.container {
width: 200px;
height: 200px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border: 3px solid #333;
}
.tracker {
width: 10px;
height: 10px;
left: 0;
top: 0;
background: red;
border-radius: 1000px;
transform: translate(calc(-50% + 200px * var(--x)), calc(-50% + 200px * var(--y)));
transition: transform 0.1s;
}
JavaScript
const pos = { x : 0, y : 0 };
const saveCursorPosition = function(x, y) {
pos.x = (x / window.innerWidth).toFixed(2);
pos.y = (y / window.innerHeight).toFixed(2);
document.documentElement.style.setProperty('--x', pos.x);
document.documentElement.style.setProperty('--y', pos.y);
}
document.addEventListener('mousemove', e => { saveCursorPosition(e.clientX, e.clientY); })