Skip to content

Instantly share code, notes, and snippets.

@jbmyid
Created August 5, 2015 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jbmyid/fe9f76a6c222b4541aeb to your computer and use it in GitHub Desktop.
Save jbmyid/fe9f76a6c222b4541aeb to your computer and use it in GitHub Desktop.
(function() {
var isIE = /msie/gi.test(navigator.userAgent); // http://pipwerks.com/2011/05/18/sniffing-internet-explorer-via-javascript/
this.infiniteScroll = function(options) {
var defaults = {
callback: function() {},
distance: 50
}
// Populate defaults
for (var key in defaults) {
if(typeof options[key] == 'undefined') options[key] = defaults[key];
}
var scroller = {
options: options,
updateInitiated: false
}
window.onscroll = function(event) {
handleScroll(scroller, event);
}
// For touch devices, try to detect scrolling by touching
document.ontouchmove = function(event) {
handleScroll(scroller, event);
}
}
function getScrollPos() {
// Handle scroll position in case of IE differently
if (isIE) {
return document.documentElement.scrollTop;
} else {
return window.pageYOffset;
}
}
var prevScrollPos = getScrollPos();
// Respond to scroll events
function handleScroll(scroller, event) {
if (scroller.updateInitiated) {
return;
}
var scrollPos = getScrollPos();
if (scrollPos == prevScrollPos) {
return; // nothing to do
}
// Find the pageHeight and clientHeight(the no. of pixels to scroll to make the scrollbar reach max pos)
var pageHeight = document.documentElement.scrollHeight;
var clientHeight = document.documentElement.clientHeight;
// Check if scroll bar position is just 50px above the max, if yes, initiate an update
if (pageHeight - (scrollPos + clientHeight) < scroller.options.distance) {
scroller.updateInitiated = true;
scroller.options.callback(function() {
scroller.updateInitiated = false;
});
}
prevScrollPos = scrollPos;
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment