Jump to content

Display Mouse Position

Recommended Posts

I have a page that works here:


I am looking for a way to continuously display in the menu at the top of the page the mouse position at all times except possible when a mouse button is being pressed.  Google and Bing seem to give me only similar subjects - like the position when hovering over a specific thing.  Any leads would be greatly appreciated.

Thank you for your help.

Jim Adrian





Link to post
Share on other sites

You would need a mousemove event on the <body> element. Using that event, you would have to find the coordinates of the mouse relative to the page using the pageX and pageY event properties. Here's a quick example:

document.body.addEventListener("mousemove", checkMousePosition, false);
function checkMousePosition(e) {
  console.log(e.pageX, e.pageY);

If an element is a direct child of the body, you can set its position to absolute using CSS and its top and left properties will be relative to the page. Then you can use the mouse event to set the element's top and left properties:

// You should store a reference to the element that will follow the mouse into the "element" variable
element.style.top = e.pageX + "px";
element.style.left = e.pageY + "px";


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...