views:

23

answers:

2

Hi Guys,

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

A: 

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

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

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

Nick Craver
You guys are awesome. That did it! Thank YOU!!
@lucl - Welcome :) And welcome to SO!
Nick Craver
A: 

How about the document.body.scrollTop attribute, this contains the pixels you have scrolled. I believe a simple pageY - scrollTop should suffice then?

jpluijmers