PDA

View Full Version : Coords.


Mhtml
03-23-2003, 08:37 AM
How can I get the X/Y coords of the mouse cursor relative to an image?

IE; 0,0 will be the top left of the image, not the page.

brothercake
03-23-2003, 09:11 AM
Use a recursive function to find the co-ordinates of the image. Then by comparison with the mouse's page co-ordinates you'll have the its x,y position relative to the image :)

Mhtml
03-23-2003, 09:14 AM
How can I get the image's coords? lol, I'm not terribly good at javascript. :)

EDIT: I can get coords of mouse though.

brothercake
03-23-2003, 09:16 AM
Oh right, you need something like this

var elePos;

function getRealCoords(ele,axis)
{
(axis=="x") ? elePos = ele.offsetLeft : elePos = ele.offsetTop;
tempEle = ele.offsetParent;
while(tempEle != null)
{
elePos += (axis=="x") ? tempEle.offsetLeft : tempEle.offsetTop;
tempEle = tempEle.offsetParent;
}
return elePos;
}

Which you call like this:

var imgX = getRealCoords(imgObj,"x")
var imgY = getRealCoords(imgObj,"y")

Mhtml
03-23-2003, 09:23 AM
Yahoo!, thanks BC!