Posted By

MichaelM on 06/25/10


Tagged

mouse position


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

laurentzziu
dipser


Mouse Position


 / Published in: JavaScript
 

Just a short and simple way of finding the mouse position.

  1. mousePosition = function(){
  2. var e = e||window.event;
  3. return {x:e.clientX,y:e.clientY}
  4. }
  5.  
  6. // Called in the following way:
  7.  
  8. mousePosition().x
  9. //or
  10. mousePosition().y
  11.  
  12. //If you want to take into account the scroll position you can also do the following:
  13.  
  14. mousePosition = function(){
  15. var e = e||window.event,dp = document.body.parentElement,ofy = window.pageYOffset, ofx = window.pageXOffset;
  16. return {x:e.clientX+parseInt(ofx?ofx:dp.scrollLeft),y:e.clientY+parseInt(ofy?ofy:dp.scrollTop)}
  17. }

Report this snippet  

You need to login to post a comment.