Victor
Victor

Reputation: 14622

Get the XY coordinates of a `img` element

I have an icon <img src="res/img/b_drop.png" id="drop_1" />. When this icon is clicked, I need a prompt to appear at some coordinates that I will calculate by the S and Y points of my #drop_. What can I do to calculate these two points using jQuery?

Upvotes: 0

Views: 134

Answers (2)

iConnor
iConnor

Reputation: 20209

offset could be what you want

$('#drop_1').on('click', function(){
    var offset = $(this).offset();
    alert('top - ' + offset.top + "\n left - " + offset.left);
});

This will alert the position of the element from the top and left of the document

jQuery offset()

Here is a

Demo

Upvotes: 3

maximkou
maximkou

Reputation: 5332

Relatively parent element:

$('#drop_1').position().left // x coord
$('#drop_1').position().top // y coord

Relatively page:

$('#drop_1').offset().left // x coord
$('#drop_1').offset().top // y coord

Upvotes: 0

Related Questions