I have a mousedown
event handler on an img
element:
$('#some_image').bind({
mousedown: function(e) {
var img = e.target;
var relpos = ???;
}
})
How can I get the location of the mousedown
relative to img
? Relative, that is, to any one of img
's corners, whichever is easiest.
FWIW, I'm using jQuery.
(Sorry for the dumb question. I imagine the answer must be trivial, but I can't find it, and it's driving me insane...)
EDIT: OK, here's the answer:
$('#some_image').bind({
mousedown: function(e) {
var offset = $(this).offset();
var relpos = [e.pageX - offset.left, e.pageY - offset.top];
// etc.
}
})
Actually, I found that for what I'm doing it's better to subtract Math.round(offset.left)
and Math.round(offset.top)
rather than the raw offsets, since the latter are not always integers (go figure).
BTW, at least according to Firebug, the event's offsetX
and offsetY
are undefined, and layerX
and layerY
are not even listed among its members.