views:

1339

answers:

3

Good day everyone.

I am working on a Firefox extension, and I want to pop up a tooltip at a certain offset from the mouse cursor. However, the problem comes when this offset is out of the viewport. It gets displayed but the user will have to scroll over there. I hope to enhance this by moving the tooltip pop-up within the current viewport.

However, a problem arises because the only clue I have to where I am in the document is the mouse-position. A partial solution would be to calculate how much to move my tooltip by finding out if the current mouse coordinate + the tooltip width/height and see if it will exceed window.innerHeight or window.innerWidth.

However, I come to realize that if it was a very long document and the user scrolled down a fair bit, the mouse coordinate would have a very large y value. Therefore, I can't rely solely on window.innerHeight to see if I am still within the viewport. Anyone found a way to find out the mouse coordinate of the top left corner in the viewport if the user has scrolled down a lot?

Thank you in advance! =)

+1  A: 

I think you are looking for something like the scrollTop property:

scrollTop gets or sets the number of pixels that the content of an element is scrolled upward.

slosd
Thank you for the clue. Will investigate further.
wai
+2  A: 

More specifically in your case, document.body.scrollTop.

However, that's pretty IE-specific, which defeats the purpose of most FireFox extensions. ;-) There are also some DTD dependencies to boot.

This looks like what you want: Determining browser dimensions and document scroll offsets

DreadPirateShawn
I saw the answer above before I went to bed last night and I tried to experiment with it. However, it got too early in the morning and I fell asleep before managed to reply. I was trying to get the scrollTop value but for some unknown reason, it always returned zero. window.pageXOffset and window.pageYOffset hit the nail straight in the head. Thank you very much! =) I learned something new. I'm happy and would upvote this twice if I could.
wai
A: 

@WAI - Well what you said is correct but precisely you will have to use the following if you are working for firefox extension:

window.content.pageXOffset 

OR

window.content.pageYOffset
Meher Ranjan