Js: Mouse Coordinates Relative To An Element
Is it cross-browser to catch the mouse coordinates relative to a div box with this: pos_x = event.offsetX?(event.offsetX):event.pageX-document.getElementById('thebox').offsetLeft;
Solution 1:
this works for me; change to your framework:
functionassignPosition(element,event) {
cX=event.clientX; cY=event.clientY;
if ($$(element).pageYOffset)
{
rX=$$(element).pageXOffset;
rY=$$(element).pageYOffset;
}
if (document.body)
{
rX=document.body.scrollLeft;
rY=document.body.scrollTop;
}
if (document.documentElement && document.documentElement.scrollTop)
{
rX=document.documentElement.scrollLeft;
rY=document.documentElement.scrollTop;
}
cX+=rX;
cY+=rY;
$$(element).style.left=cX+"px";
$$(element).style.top=cY+"px";
}
Solution 2:
In case you do not have all/most of the browser types/versions to test on, you might take a look at this: W3C DOM Compatibility - Events Linked from above: W3C DOM Compatibility - CSS Object Model View
Gives you an idea of how compatible the codes are.
Solution 3:
functiondodoubleclick(e){
var mouseX, mouseY;
if(e.offsetX) {
mouseX = e.offsetX;
mouseY = e.offsetY;
}
elseif(e.layerX) {
mouseX = e.layerX;
mouseY = e.layerY;
}
alert("mousex:"+mouseX+"and"+"mousey:"+mouseY);
}
this snippet will gives you mouse coordinates
Post a Comment for "Js: Mouse Coordinates Relative To An Element"