Mouse position inside an element


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



Copy this code and paste it in your HTML
  1. if(window.addEventListener) {
  2. element.addEventListener('mousemove', handleMouseClick, false);
  3. } else {
  4. element.attachEvent('onmousemove', handleMouseClick);
  5. }
  6.  
  7. function handleMouseClick(e) {
  8. if(window.event) e = window.event;
  9.  
  10. // mouse position relative to the top of the document
  11. var xpos = e.pageX - this.offsetLeft;
  12. var ypos = e.pageY - this.offsetTop;
  13.  
  14. // mouse position relative to the viewport
  15. //var xpos = e.clientX - this.offsetLeft;
  16. //var ypos = e.clientY - this.offsetTop;
  17. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.