質問

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

役に立ちましたか?

解決

Yes

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

他のヒント

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>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top