James Hall
James Hall

Reputation: 6689

JQuery Object has offsetTop value different than what offset function returns

So i'm trying to figure out something going on with jQuery's offSet function and the actual objects offset. When I do console.log for my object it shows an offsetTop of 21294. However when I do the jQuery function call for offset().top, im getting 22069. The body had no padding or margin, so im at a bit of a loss of why everything is returning so differently. There is a top margin of 30 on the object, but that is about it.

offsetTop is set to 21294

Upvotes: 0

Views: 1024

Answers (1)

dave
dave

Reputation: 64667

They do different things.

From jQuery's site for offset:

Get the current coordinates of the first element, or set the coordinates of every element, in the set of matched elements, relative to the document.

From Mozilla's JS docs on offsetTop:

The HTMLElement.offsetTop read-only property returns the distance of the current element relative to the top of the offsetParent node.

So you are measuring one relative to the document, one relative to the offsetParent node.

Typically a positioned div or table will act as an offset container to any element contained inside them.

In your case it appears to be div#resizable.main-content

Upvotes: 2

Related Questions