Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
42
rated 0 times [  46] [ 4]  / answers: 1 / hits: 95296  / 15 Years ago, tue, june 16, 2009, 12:00:00

I need to get the X,Y coordinates (relative to the document's top/left) for a DOM element. I can't locate any plugins or jQuery property or method that can give these to me. I can get the top and left of the DOM element, but that can be either relative to its current container/parent or to document.


More From » jquery

 Answers
41

you can use Dimensions plugin [Deprecated... included in jQuery 1.3.2+]




offset()

Get the current offset of the first matched element, in pixels, relative to the document.



position()
Gets the top and left position of an element relative to its offset parent.




knowing this, then it's easy... (using my little svg project as an example page)



var x = $(#wrapper2).offset().left;
var y = $(#wrapper2).offset().top;

console.log('x: ' + x + ' y: ' + y);


output:



x: 53 y: 177


hope it helps what you're looking for.



here's an image of offset() and position()



using XRay



alt



using Web Developer toolbar



alt


[#99303] Friday, June 12, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaitlyn

Total Points: 421
Total Questions: 73
Total Answers: 100

Location: South Georgia
Member since Sat, Jul 25, 2020
4 Years ago
;