Skip to content

Instantly share code, notes, and snippets.

@tennisonchan
Created April 18, 2015 07:57
Show Gist options
  • Save tennisonchan/8b76a27f1db84571422e to your computer and use it in GitHub Desktop.
Save tennisonchan/8b76a27f1db84571422e to your computer and use it in GitHub Desktop.
Detect user scroll position on page
var timeout20 = null;
var timeout50 = null;
onscroll = function(e){
var scrollPosition = window.scrollY / (window.document.body.scrollHeight - window.innerHeight);
if(0.2 < scrollPosition && scrollPosition < 0.5){
clearTimeout(timeout20);
timeout20 = setTimeout(function(){
console.log('20%');
}, 500);
}
if(0.5 < scrollPosition && scrollPosition < 1){
clearTimeout(timeout50);
timeout50 = setTimeout(function(){
console.log('50%');
}, 500);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment