Skip to content

Instantly share code, notes, and snippets.

@shawnchin
Created April 5, 2011 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shawnchin/903887 to your computer and use it in GitHub Desktop.
Save shawnchin/903887 to your computer and use it in GitHub Desktop.
/* scroll page so obj is visible in current view with
* a _minimum_ bottom margin of "bottom_margin" (default:20).
* -- requires jQuery --
*/
function scroll_to(obj, bottom_margin, speed) {
bottom_margin = bottom_margin || 20;
var target_height = obj.height() + bottom_margin;
var visible_height = $(window).height();
var scroll_top = obj.offset().top;
if (target_height < visible_height) {
scroll_top += target_height - visible_height;
}
$('html').animate({scrollTop:scroll_top}, (speed)? speed : "fast");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment