skip to Main Content

I have handleScroll callback

<div  onScroll={handleScroll} >

And handleScroll is called when mouse whell is touched.

const handleScroll = (e) =>{
    console.log("handlescroll clientX",e.clientX);
    console.log("handlescroll clientY",e.clientY);

However it doesn’t have mouse poitner position.

So, I think there are two methods.

1 Get the mouse pointer position in scroll callback

2 Get the mouse pointer potition by spontaneously (not by callback)

Either way is possible?

2

Answers


  1. The only way to get the mouse position is with events. There is no way to get it without callbacks.

    You can see more here: How to get the mouse position without events (without moving the mouse)?

    Login or Signup to reply.
  2. You are doing it the right way.

    For the second method you can do it without using callbacks.

    document.addEventListener('mousemove', (e) => {
        console.log("Mouse X:", e.clientX);
        console.log("Mouse Y:", e.clientY);
    });
    

    Do let me know how it works for you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search