Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the mouse position relative to the window viewport in javascript?

event.pageY gets you the mouse position relative to the entire document height(document.documentElement.offsetHeight I assume).

But how do I get the mouse position relative to the current viewport, which is document.documentElement.clientHeight?

For example, if the browser window size has a 720 pixel height, I scroll down 3 pages and keep the mouse in the middle of the window, the position should be "360", not 1800 (720 x 3 - 720 / 2).

like image 624
nice ass Avatar asked Feb 05 '13 21:02

nice ass


People also ask

How can I see my mouse position on my screen?

In Mouse Properties, on the Pointer Options tab, at the bottom, select Show location of pointer when I press the CTRL key, and then select OK.

How do you get the mouse position in Qt?

To set or get the position of the mouse cursor use the static methods QCursor::pos() and QCursor::setPos().

Can you move the mouse with Javascript?

You can't move the mouse pointer using javascript, and thus for obvious security reasons. The best way to achieve this effect would be to actually place the control under the mouse pointer. The security implications are far from obvious.


1 Answers

Try using event.clientY that should always return the correct value regardless of scrolling

https://developer.mozilla.org/en-US/docs/DOM/event.clientY

like image 189
lostsource Avatar answered Sep 22 '22 08:09

lostsource