javascript - JS pageX and pageY take the coordinate plus the margin of relative position div? -


i have div want center use

margin-left: auto; margin-right: auto; position: relative; 

that's easiest way center admit, when want use event.pagex , event.pagey takes coordinates plus left margin , that's wrong.

here fiddle. click somewhere on green rectangle watch result :

http://jsfiddle.net/fgkuq/

any ideas how fix show square coordinates without left margin ?

take @ updated fiddle.

quick solution remove positioning of canvas:

#canvasdiv {      width: 30%;     height: 400px;       cursor:crosshair;       background-color: #458b00;      /* position: relative; */       ... } 

the problem positioning of template. because absolute still "relative".

the definition of absolute positioning: an absolute position element positioned relative first parent element has position other static.

therefore position of #template take consideration position of #canvasdiv unless leave #canvasdiv default static positioning.

learn more positioning @ w3schools

positioning absolutely elements inside relatively positioned elements: here

great example of problem tabs 3 & 4.

should wish stick relative positioning of canvasdiv, have update script, takes account positioning of canvasdiv:

var x = event.pagex;   var y = event.pagey;    var canvaspos = $(this).offset(); // in context of script refers #canvasdiv var styles = {    "left" : x - canvaspos.left, // remove left offset    "top" : y - canvaspos.top // remove top offset }; 

check out fiddle


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -