Skip to content

Instantly share code, notes, and snippets.

@andys
Created December 29, 2010 06:55
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 andys/758283 to your computer and use it in GitHub Desktop.
Save andys/758283 to your computer and use it in GitHub Desktop.
javascript scroll to an element if it isn't visible
function scroll_to_if_not_visible(finder) {
var postop = $(finder).offset().top, win = $(window);
var posbottom = postop + $(finder).height();
if (posbottom > (win.height() + win.scrollTop())) {
$(finder).get(0).scrollIntoView(false);
}
else if (postop < win.scrollTop()) {
$(finder).get(0).scrollIntoView(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment