Question

Is there a way jQuery can get a pixel value of height from start of the document to a certain element? A way without counting each element individually? Thanks

Était-ce utile?

La solution

Yes

$("#idelement").offset().top;

Autres conseils

You can use jQuery's offset().

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

As the description mentions, it gets the distance in pixels between the start of the page (document) and the element.

Also as noted in the API documentation, the element you are trying to get the offset from, must be visible.

offset() returns a javascript object containing 2 properties: top and left. What you want it the top property.

Usage example from the link:

<script>
var p = $( "p:last" );
var offset = p.offset();
p.html( "left: " + offset.left + ", top: " + offset.top );
</script>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top