Get cursor position relative to the target of the element. - Node.js DOM

Node.js examples for DOM:Element

Description

Get cursor position relative to the target of the element.

Demo Code


/*/* w w w .  j  av a2s  .  c o m*/
 * Get cursor positon relative to the target of the element.
 */
function getCursorPosition(e){
  var x;
  var y;
  if (e.pageX != undefined && e.pageY != undefined) {
    x = e.pageX;
    y = e.pageY;
  }
  else {
    x = e.clientX + document.body.scrollLeft +
      document.documentElement.scrollLeft;
    y = e.clientY + document.body.scrollTop +
      document.documentElement.scrollTop;
  }
  x -= e.target.offsetLeft;
  y -= e.target.offsetTop;
  return {target: e.target,x: x,y: y};
};

Related Tutorials