Pregunta

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

¿Fue útil?

Solución

Yes

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

Otros consejos

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>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top