I wish to achieve content load when the user scrolls to the bottom of the page.
I am having a problem. It works fine on desktop browsers but not on mobile. I have imple
This is what worked for me:
(window.innerHeight + window.scrollY) == $(document).height()
Found here: Javascript: How to detect if browser window is scrolled to bottom?
you can also use an endless scroll plugin: https://github.com/fredwu/jquery-endless-scroll https://github.com/paulirish/infinite-scroll
i'm using the endless scroll and it works great on my laptop and iphone
An improved jQuery version of the code
var scrollRefresh = {
pastTop: false,
pastBottom: false,
previous: 0,
bottom: function(callback) {
var pBottom = $(window).height() + $(window).scrollTop() >= $(document).height();
if(!this.pastBottom && pBottom) {
callback($(window).height() + $(window).scrollTop());
this.pastBottom = true;
} else {
if(!pBottom) this.pastBottom = false;
}
this.previous = $(window).scrollTop();
},
top: function(callback) {
var pTop = $(window).scrollTop() < this.scrollPrevious && $(window).scrollTop <= 0;
if(!this.pastTop && pTop) {
callback($(window).scrollTop());
this.pastTop = true;
} else {
if(!pTop) this.pastTop = false;
}
this.previous = $(window).scrollTop();
}
}
$(window).scroll(function() {
scrollRefresh.top(function(pos) {
console.log("Loading top. " + pos);
alert("scrolled to top"); //You should delete this line
});
scrollRefresh.bottom(function(pos) {
console.log("Loading bottom. " + pos);
alert("scrolled to bottom"); //You should delete this line
});
});
Though the question was asked 5 years ago, still it is more than relevant in today's UI/UX context. And I would like to add my two cents.
var element = document.getElementById('flux');
if (element.scrollHeight - element.scrollTop === element.clientHeight)
{
// element is at the end of its scroll, load more content
}
Source: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight#Determine_if_an_element_has_been_totally_scrolled
Here is an aggregate of the other answers which works very well for me. It could be written as a jQuery plugin sometime.
// Handles scrolling past the window up or down to refresh or load more data.
var scrolledPastTop = false;
var scrolledPastBottom = false;
var scrollPrevious = 0;
function getDocumentHeight() {
return Math.max(
Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
Math.max(document.body.clientHeight, document.documentElement.clientHeight)
);
}
function bottomScrollRefresh(callback) {
var pastBottom = $window.height() + $window.scrollTop() > getDocumentHeight();
if(!scrolledPastBottom && pastBottom) {
callback($window.height() + $window.scrollTop());
scrolledPastBottom = true;
} else {
if(!pastBottom) scrolledPastBottom = false;
}
scrollPrevious = $window.scrollTop();
}
function topScrollRefresh(callback) {
var pastTop = $window.scrollTop() < scrollPrevious && $window.scrollTop() <= 0;
if(!scrolledPastTop && pastTop) {
callback($window.scrollTop());
scrolledPastTop = true;
} else {
if(!pastTop) scrolledPastTop = false;
}
scrollPrevious = $window.scrollTop();
}
To use this in your code, add something like this:
window.onscroll = function() {
topScrollRefresh(function(pos) {
console.log("Loading top. " + pos);
});
bottomScrollRefresh(function(pos) {
console.log("Loading bottom. " + pos);
});
};
Works great for my PhoneGap/Cordova app.
If you can, you should really avoid trying to hit an exact number.
Using Stelok's function above, you can test like this:
if ($win.height() + $win.scrollTop() > (getDocumentHeight()-10)) {
It's a bit softer - the user may not have quite scrolled to the exact last pixel but thinks he/she is at the bottom.