Skip to content

Instantly share code, notes, and snippets.

@acrookston acrookston/scroll.js forked from arnorhs/scroll.js
Created May 17, 2012

What would you like to do?
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);
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
You can’t perform that action at this time.