Witam, mam problem z javascriptem. Otóż poniższy kod powinien wyświetlać element o id = 'v' w losowych miejscach na stronie co 500ms, jednak nie wiem dlaczego elementy zmieniają pozycję dużo szybciej.
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel = 'stylesheet' href = 'style.css'>
<script src = 'main.js' defer></script>
</head>
<body>
<div id = 'x'></div>
<div id = 'y'></div>
<div id = 'z'></div>
<div id = 'v'></div>
</body>
</html>
CSS:
body{
overflow: hidden;
}
#z{
background-color: black;
width: 20px;
height: 20px;
border-radius: 50%;
}
#v{
background-color: blue;
width: 30px;
height: 30px;
border-radius: 50%;
position: fixed;
}
JS:
window.addEventListener('mousemove', function (e){
let x = document.getElementById('x');
let y = document.getElementById('y');
let z = document.getElementById('z');
let v = document.getElementById('v');
if(e.x !== null && e.y !== null){
setTimeout(function(){
z.style.position = 'absolute';
z.style.left = e.x + 'px';
z.style.top = e.y + 'px';
},50);
}
let width = window.innerWidth;
let height = window.innerHeight;
setInterval(randomize,500);
function randomize(){
let randomWidth = Math.floor(Math.random() * width);
let randomHeight = Math.floor(Math.random() * height);
v.style.top = randomWidth + 'px';
v.style.left = randomHeight + 'px';
}
});