Find position of element on scroll

I’m trying to learn how to get the position of an element with a specific class.

I keep getting the answer of zero. Why?

            <p class="hello">Yes</p>

	<script>

$(document).scroll(function(){

			var hello = $( ".hello" );
			$( "p:nth-child(7)" ).text( "scrollTop:" + hello.scrollTop() );
			
 });


</script>

jQuery provides .position() and .offset() that give you the position of the element. Position gives you in relation to its offset parent, and Offset gives it to you relative to the entire page.

.scrollTop() gives you how far the entire page has been scrolled down, in effect the scrollbar itself, which in most simple tests will be zero and only becomes larger when there is content below screen.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.