문제

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