Skip to content

Instantly share code, notes, and snippets.

@arnorhs
Forked from acrookston/scroll.js
Created May 17, 2012 08:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arnorhs/2717337 to your computer and use it in GitHub Desktop.
Save arnorhs/2717337 to your computer and use it in GitHub Desktop.
Small scrolling script for fun. Not sure how well it works. Please contribute if you have ideas.
// muhaha.. changed all the code
// maybe this version handles scrolling to the bottom edge of a document a little better
// still not satisfied with the speed variable.. should that be higher == more speed, perhaps? or pixels per second?
// sorry about the opinionated style changes
Scrolling = {
smoothScrollTo: function(target_top) {
// ensure that we never scroll further than viewport size from bottom of the doc
target_top = Math.min(target_top, Math.max($(document).height(), $(window).height()) - $(window).height());
var speed = 30,
window_top = $(window).scrollTop(),
each_step = (target_top - window_top) / speed;
var scroll = function() {
$(window).scrollTop(window_top += each_step);
// there is a cleaner way to do this check
if (Math.floor(window_top / speed) != Math.floor(target_top / speed)) {
setTimeout(scroll, 13);
}
}
scroll();
},
smoothScrollToBottom: function() {
Scrolling.smoothScrollTo($(document).height() - $(window).height());
},
scrollToBottom: function() {
$(window).scrollTop($(document).height() - $(window).height());
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment