Cross Browser Cursor Position


/ Published in: JavaScript
Save to your folder(s)

Nice little snippet i found on Ajaxian, finds the position of the mouse cursor when it is fired.


Copy this code and paste it in your HTML
  1. function getPosition(e) {
  2. e = e || window.event;
  3. var cursor = {x:0, y:0};
  4. if (e.pageX || e.pageY) {
  5. cursor.x = e.pageX;
  6. cursor.y = e.pageY;
  7. }
  8. else {
  9. cursor.x = e.clientX +
  10. (document.documentElement.scrollLeft ||
  11. document.body.scrollLeft) -
  12. document.documentElement.clientLeft;
  13. cursor.y = e.clientY +
  14. (document.documentElement.scrollTop ||
  15. document.body.scrollTop) -
  16. document.documentElement.clientTop;
  17. }
  18. return cursor;
  19. }

URL: http://ajaxian.com/archives/javascript-tip-cross-browser-cursor-positioning

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.