Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@acrookston
Created May 16, 2012 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save acrookston/2714188 to your computer and use it in GitHub Desktop.
Save acrookston/2714188 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.
Scrolling = {};
$.extend(Scrolling, {
smoothScrollTo: function(top) {
var sm_speed = 300;
var sm_runs = 0;
var sm_last = -1; // don't use 0, b/c sm_window_top can be 0
var sm_window_top = $(window).scrollTop();
var sm_down = (top > sm_window_top);
var sm_each_step = (sm_down ? top - sm_window_top : sm_window_top - top) / (sm_speed / 13);
var sm_scroll = function() {
sm_window_top = $(window).scrollTop();
$(window).scrollTop(sm_window_top + (sm_down ? sm_each_step : -(sm_each_step)));
if (sm_window_top != sm_last && sm_runs < 1000 && (sm_down && sm_window_top < top || !sm_down && sm_window_top > top)) {
sm_runs += 1;
sm_last = sm_window_top;
setTimeout(sm_scroll, 13);
}
}
sm_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