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

micmath on 04/14/08


Tagged


Versions (?)


Get mouse coordinates relative to document


Published in: JavaScript 


URL: http://evolt.org/article/Mission_Impossible_mouse_position/17/23335/index.html

Snagged from pp]{ It's relative to document, not the visible area in the window.

  1. function handleEvent(e)
  2. {
  3. if (!e) var e = window.event;
  4. var posx = 0;
  5. var posy = 0;
  6. if (!e) var e = window.event;
  7. if (e.pageX || e.pageY)
  8. {
  9. posx = e.pageX;
  10. posy = e.pageY;
  11. }
  12. else if (e.clientX || e.clientY)
  13. {
  14. posx = e.clientX + document.body.scrollLeft;
  15. posy = e.clientY + document.body.scrollTop;
  16. }
  17. // posx and posy contain
  18. // the mouse position relative to the document
  19. // Do something with this information
  20. }

Report this snippet 

You need to login to post a comment.