1

So while making a website for trolls, I had the idea to make the website move a button that takes the user to another website and clicking it is the only way forward. Is there a way to check for the location of the cursor on the website to make the button change location, or is there a way to randomize the location of the cursor when it gets into a specific range?

Ironworld
  • 11
  • 2

2 Answers2

3

You can detect the location of the mouse by listening to the onmousemove event.

document.onmousemove = (ev) => {
  if (ev.clientX < 100) {
    document.querySelector('button').classList.add('moved');
  } else {
    document.querySelector('button').classList.remove('moved');
  }
}
.moved {
  transform: translate(200px);
}
<button>Click me!</button>
Olian04
  • 6,480
  • 2
  • 27
  • 54
1

all you need is onmousemove global handler. But you must be very careful - every even small move of mouse calls the event, so you function should not be very heavy. There is a way to prevent any problems by using throttling.

Tarukami
  • 1,160
  • 5
  • 8