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?

3
  • There is a possibility to get the pointer location but not to manipulate it. Commented Jul 25, 2020 at 10:45
  • Read up for that on: stackoverflow.com/questions/7790725/… Commented Jul 25, 2020 at 10:46
  • One thing you could do though is to hide the cursor and draw a fake one that you can control. Commented Jul 25, 2020 at 11:34

2 Answers 2

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>

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.