Created
December 29, 2010 06:55
-
-
Save andys/758283 to your computer and use it in GitHub Desktop.
javascript scroll to an element if it isn't visible
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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