Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery mouse position relative window

I'm trying to get the exact mouse position relative to the window.

Here is my issue...

  • document.height = 1600 (actual document size)
  • window.height = 400 (viewable)

I need to figure out the mouse position relative to the window, not to the document which the pageY attribute provides.

It's for a large tooltip, which gets popped in on mouesover for a table item. If there is not enough room at the bottom of the screen (window is maxed), then the tooltip get displayed above the pointer, otherwise, below the pointer. This works fine until the document size is greater than pagesize (long table).

Thanks, Luc

like image 707
user425772 Avatar asked Aug 19 '10 21:08

user425772


1 Answers

You can subtract .scrollTop() of the window from pageY to get the position in the window, like this:

var top = e.pageY - $(window).scrollTop();

You can give it a try here, take a look at the console.

like image 162
Nick Craver Avatar answered Oct 07 '22 01:10

Nick Craver