I need to get a hold of the absolute mouse position / coordinates (X and Y) using (preferably) jQuery like in this tutorial but outside of any JavaScript event. Thank you.
相关问题
- How to fix IE ClearType + jQuery opacity problem i
- jQuery add and remove delay
- Include empty value fields in jQuery .serialize()
- Disable Browser onUnload on certain links?
- how to get selected text from iframe with javascri
This started as a comment on Chetan Sastry's answer, but I realized it also might be worth posting as an answer:
I'd be careful about having a document-level, always-running
mousemove
event, even if you're only polling for cursor position. This is a lot of processing and can bog down any browser, particularly slower ones like IE.A problem like this almost certainly raises the question of design decision: if you don't need to handle a mouse event to poll for the cursor position, do you really need the cursor position? Is there a better way to solve the problem you're trying to solve?
Edit: even in Safari 4, which is (understatement) very fast, that single
mousemove
event makes every interaction with that tutorial page noticeably choppy for me. Think about how that will affect users' perceptions of your site or application.This function will decrease the impact on UI performance by only getting the mouse position at an interval:
Just as in the other answer "You can now use
window.mouseXPos
andwindow.mouseYPos
from anywhere."You lose a little accuracy as the mouse move won't be detected during the intervals.
I have try @Chetan Sastry 's soulution,but it does't works.(I'm using jQuery 1.6.4). I change the code and then i works now. Here is my code. I hope this will help.
Not possible. You can however use the same approach in the tutorial to store the position in a global variable and read it outside the event.
Like this:
You can now use
window.mouseXPos
andwindow.mouseYPos
from anywhere.