javascript - Get Mouse Pointer coordinates relative to some div -
i trying mouse pointer position on mousedown , mouseup event. there div named test , want position when mousedown , mouseup happen within div area. , taking div surface mousedown , mouseup position should related div. have pdf inside div so, coordinates me highlight pdf.
i tried this, not working fine.
function getposition(element) { var xposition = 0; var yposition = 0; while (element) { xposition += (element.offsetleft - element.scrollleft + element.clientleft); yposition += (element.offsettop - element.scrolltop + element.clienttop); element = element.offsetparent; } return { x: xposition, y: yposition }; } $("#test").mousedown(function(e){ var parentposition = getposition(e.currenttarget); startx = e.clientx - parentposition.x; starty = (e.clienty - parentposition.y)*2.5; }); $("#test").mouseup(function(e){ var parentposition = getposition(e.currenttarget); endx = e.clientx - parentposition.x; endy= (e.clienty - parentposition.y)*2.5; });
the coordinates of mouse relative div stored in offsetx
, offsety
properties of event object
$("#somediv").click(function(e){ var x = e.offsetx; var y = e.offsety; });
so if have div width of 100 , height of 50, , click in center of div then
x = 50, y = 25
Comments
Post a Comment