Get the current position of the cursor on the page
Phong Duong

Phong Duong @phongduong

About: Developer - Learner - Creator

Location:
Vietnam
Joined:
Jul 13, 2018

Get the current position of the cursor on the page

Publish Date: Nov 19 '20
0 0

To get the current position of the cursor on the page, you retrieve pageX and pageY property of the MouseEvent interface. These properties are the X and Y coordinates of the event on the document. They are in pixels and relative to the left and top edges of the document.

document.addEventListener("mousemove", ({ pageX, pageY }) => {
   console.log(pageX, pageY)
});
Enter fullscreen mode Exit fullscreen mode

These positions can change if you scroll the page. For example: if you scroll the page down 800px and move the cursor to 200px from the top edge of the document, pageY returns 1000.

Comments 0 total

    Add comment