问题
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:
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
回答2:
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.
回答3:
@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
来源:https://stackoverflow.com/questions/1045456/firefox-get-mouse-coordinates-of-top-left-corner-of-viewport