I have been trying to get a div element to follow my mouse with javascript, but it keeps glitching to the upper left hand corner. Here is my code: HTML:
<html>
<body>
<div id="circle"></div>
</body>
</html>
CSS:
body{
margin:0;
padding:0;
overflow:hidden;
background-color:#77dd77;
}
div{
position:absolute;
transform:translate(-50%,-50%);
height:35px;
width:35px;
border-radius:50%;
border:2px solid black;
}
JavaScript:
document.addEventListener('mousemove', function(e) {
let body = document.querySelector('body');
let circle = document.getElementById('circle');
let left = e.offsetX;
let top = e.offsetY;
circle.style.left = left + 'px';
circle.style.top = top + 'px';
});
I'm still having a hard time figuring out if the error is occuring in the CSS or JavaScript. Can someone help?