Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining when scrolled to bottom of a page with Javascript

I'm trying to determine when I've scrolled to the bottom of the page (without using any JS library), but so far, I'm a bit confused over which one of the following to use. The most promising one I've seen is window.scrollY, but even when scrolled to the bottom of the page, it never matches the value of window.innerHeight. What's the best way to do this?

window.innerWidth
window.innerHeight

window.outerWidth
window.outerHeight

window.scrollX
window.scrollY

document.body.scrollWidth
document.body.scrollHeight
document.body.scrollTop
document.body.scrollLeft

document.body.offsetTop
document.body.offsetLeft
document.body.offsetWidth
document.body.offsetHeight
like image 789
chimerical Avatar asked May 12 '10 08:05

chimerical


People also ask

How do you know if an element is scrolled to the bottom?

To check if a div is scrolled all the way to the bottom with jQuery, we can call the on method with 'scroll' and a scroll event handler. then we can write: const chkScroll = (e) => { const elem = $(e. currentTarget); if (elem[0].

How do you check if a user has scrolled to the bottom of a div?

To detect when a user scrolls to bottom of div with React, we can check if the sum of the scrollTop and clientHeight properties of a scrollable element is equal to the scrollHeight property of the same element. We call the useRef hook to create a ref and we assign the returned ref to the inner div, which is scrollable.

How do I know if my scroll is ending?

To detect scroll end with JavaScript, we can listen to the scroll event. Then in the event listener, we check if offsetHeight + scrollTop is bigger than or equal to scrollHeight . We get the div with document. querySelector .


2 Answers

when window.innerHeight + document.body.scrollTop is greater than or equal to document.body.offsetHeight then you are at bottom

but since IE has issues with these properties you need to use alternative properties, like

document.documentElement.scrollTop for the scroll and document.documentElement.clientHeight for the window height

full code: http://jsbin.com/egegu3/6/edit

like image 138
Gabriele Petrioli Avatar answered Nov 03 '22 09:11

Gabriele Petrioli


Being a lazy person at heart, I would put an element to the very bottom of the DIV, and apply the "element in view" jQuery plugin on it. (Disclaimer: I have no own experience with it but it looks good.)

A slight variation of the example from the blog entry:

$('#bottomDIV').bind('inview', function (event, visible) {
  if (visible == true) {
    // element is now visible in the viewport
    highlightButtons(); // or whatever you want to do in the context
  }
});
like image 43
Pekka Avatar answered Nov 03 '22 07:11

Pekka