We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

1man on 02/14/08


Tagged

mouse cursor position


Versions (?)


Who likes this?

4 people have marked this snippet as a favorite

SpinZ
adix
joellevin
wizard04


Cross Browser Cursor Position


Published in: JavaScript 


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

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

  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. }

Report this snippet 

Comments

RSS Icon Subscribe to comments
Posted By: adix on February 15, 2008

Interesting how you and Dustin Diaz made the exact same function, isn't it? http://www.dustindiaz.com/getelementsbyclass/

Posted By: adix on February 15, 2008

just ignore my comment, it was for another someone :D sorry

You need to login to post a comment.