(function() { var el = document.querySelector("body"); var isBody = el.tagName.toLowerCase() == "body"; var threshold = 60; var atBottom = false; var detectBottom = function(e) { var innerHeight = isBody ? window.innerHeight : el.offsetHeight; console.log(el.scrollTop, innerHeight, el.scrollHeight); if (el.scrollTop + innerHeight < el.scrollHeight - threshold) return atBottom = false; if (atBottom) return; atBottom = true; console.log("do stuff when you hit bottom"); }; (isBody ? window : el).addEventListener("scroll", detectBottom); document.addEventListener("load", detectBottom); })();