Skip to content

Instantly share code, notes, and snippets.

@onestepcreative
Created March 16, 2013 19:52
Show Gist options
  • Select an option

  • Save onestepcreative/5178019 to your computer and use it in GitHub Desktop.

Select an option

Save onestepcreative/5178019 to your computer and use it in GitHub Desktop.
function scrollTop(step) {
var start = window.pageYOffset;
var count = 0;
var intervalRef = setInterval( (function(interval, curOffset) {
return function() {
curOffset -= (interval * step);
console.info("offset = " + curOffset);
window.scrollTo(0, curOffset);
console.info("pageYoffset = " + window.pageYOffset);
count++;
if(count > 150 || curOffset < 0) clearInterval(intervalRef);
}
})(step, start--), 50);
}
// scroll to the top from the middle of the page in about 5 seconds.
scrollTop(5);
// scroll to the top in about 1 second
scrollTop(15);
// scrolls to the top very fast!
scrollTop(35);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment