Skip to content

Instantly share code, notes, and snippets.

@nhunzaker
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save nhunzaker/ea86aca0f082ef20d610 to your computer and use it in GitHub Desktop.

Select an option

Save nhunzaker/ea86aca0f082ef20d610 to your computer and use it in GitHub Desktop.
(function() {
var delay = 1000 / 12;
var last = +new Date();
var step = 25;
var interval = {};
var getScrollProgress = function() {
return (document.body.scrollTop / (document.body.scrollHeight - window.innerHeight)) * 100;
};
var report = function(progress) {
console.log(progress);
};
var track = function() {
var now = +new Date();
var isComplete = interval['100'];
var tooSoon = now - last < delay;
if (isComplete || tooSoon) return;
last = now;
var progress = getScrollProgress();
for (var i = step; i <= 100; i += step) {
if (!interval[i] && progress >= i) {
interval[i] = true;
report(i);
}
}
}
window.addEventListener('scroll', track);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment