Created
November 22, 2023 08:54
-
-
Save GreXLin85/5aff5d9b392829ed12b41c1532f0521a to your computer and use it in GitHub Desktop.
Button following the cursor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>BTN</title> | |
<style> | |
button { | |
padding: 20px 20px; | |
border: none; | |
background-color: #000; | |
color: #fff; | |
font-size: 10px; | |
cursor: pointer; | |
border-radius: 50%; | |
font-weight: 900; | |
} | |
</style> | |
</head> | |
<body> | |
<script> | |
document.addEventListener("DOMContentLoaded", function () { | |
const button = document.createElement("button"); | |
button.innerHTML = "BTN"; | |
button.style.position = "absolute"; | |
button.style.left = "50%"; | |
button.style.top = "50%"; | |
button.style.transform = "translate(-50%, -50%)"; | |
document.body.appendChild(button); | |
let lastMouseX = button.clientTop; | |
let lastMouseY = button.clientLeft; | |
document.addEventListener("mousemove", function (e) { | |
setTimeout(() => { | |
button.animate( | |
[ | |
{ | |
left: lastMouseX + "px", | |
top: lastMouseY + "px", | |
}, | |
{ | |
left: e.clientX + "px", | |
top: e.clientY + "px", | |
} | |
], | |
{ | |
fill: "forwards", | |
easing: "ease-in-out", | |
} | |
); | |
lastMouseX = e.clientX; | |
lastMouseY = e.clientY; | |
}, 100); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment