Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting offset of an element on scroll in javascript / jQuery

I am looking to calculate the distance between an element and the top of the document window. On scroll I am able to get the initial value but it does not change. How can I find this value and what the number has changed to on scroll?

JS:

$(function() {
    $(window).scroll(function() {
        var h1 = $("h1");
        console.log(h1.offset().top)
    });
});

HTML:

<div id="cover">   
    <h1>hello sir</h1> 
</div>
like image 547
jeffreynolte Avatar asked Jun 23 '11 20:06

jeffreynolte


People also ask

How can I detect scroll end of the specified element by Javascript?

getElementById('box'). offsetHeight; var clientHeight = document. getElementById('box'). clientHeight; if (offsetHeight <= scrollTop + clientHeight) { // This is called before scroll end! } }, false );

What is scroll offset?

forward, then scroll offset is the amount the top of the sliver has been scrolled past the top of the viewport. This value is typically used to compute whether this sliver should still protrude into the viewport via SliverGeometry.

What is offset () top in jQuery?

The offset() method set or returns the offset coordinates for the selected elements, relative to the document. When used to return the offset: This method returns the offset coordinates of the FIRST matched element. It returns an object with 2 properties; the top and left positions in pixels.

How do I find the offset on my bottom?

The jQuery offset() function is a built-in function in jQuery, by which we can return the offset of the bottom coordinate. The jQuery offset() function only specifies the top and left properties position, but with the help of top property and outerHeight() function, we can get the bottom position for elements.


3 Answers

Compare the offset of the <h1> element to how far down the page the user has scrolled. The $(window).scrollTop() function will get you the amount the user has scrolled down so:

$(window).scroll(function() {
  var $h1 = $("h1");
  var window_offset = $h1.offset().top - $(window).scrollTop();
});
like image 95
MidnightLightning Avatar answered Oct 20 '22 13:10

MidnightLightning


If you hate arithmetic (and extra function calls), this should do the trick:

$(function() {
    var h1 = document.getElementsByTagName("h1")[0];
    $(window).scroll(function() {
        console.log(h1.getBoundingClientRect().top);
    });
});

This is exactly what getBoundingClientRect() was made for.

like image 28
Noyo Avatar answered Oct 20 '22 12:10

Noyo


You can use this function to get the scroll offset of the window:

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

Then you can use the offsets in your function to determine the real position of your element:

$(function() {
    $(window).scroll(function() {
        var h1 = $("h1");
        var offs = getScrollXY();
        console.log(h1.offset().top - offs[1]);
    });
});

Theoretically, this should work in all browsers, but, frankly, I didn't do too much testing.

like image 3
Aleks G Avatar answered Oct 20 '22 13:10

Aleks G