Impossible button
JavaScript
Medium
Objective
Create a button that attempts to avoid being clicked by moving away from the mouse cursor.
Requirements
Create a button that's initially placed in a random position in the viewport
Use JavaScript to move the button away from the mouse cursor when the cursor comes within a certain distance
Add a counter that keeps track of how many times the user successfully clicks the button
The button should not leave the viewport
Starting code
// Create your own HTML & CSS
document.addEventListener("DOMContentLoaded", () => {
const button = document.getElementById("impossibleButton");
const clickCounter = document.getElementById("clickCounter");
let counter = 0;
// Initialise button position
// TODO
// Event Listeners
// TODO
button.addEventListener("click", () => {
counter++;
clickCounter.innerText = counter;
});
});