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

noah on 06/25/07


Tagged

popup DOM mouse work ui position oo ppk utilities quirksmode coordinates balloon pointer


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

luman
heinz1959


Get Mouse Position (from Quirksmode.org)


Published in: JavaScript 


URL: http://www.quirksmode.org/js/events_properties.html#position

This is PPK's solution for finding mouse position across platforms.

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

Report this snippet 

You need to login to post a comment.