Skip to content

Instantly share code, notes, and snippets.

@jonkemp
Created June 21, 2012 18:24
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 jonkemp/2967570 to your computer and use it in GitHub Desktop.
Save jonkemp/2967570 to your computer and use it in GitHub Desktop.
scrollDown: Scrolls the page down to show an element if it is not currently in the viewport. Requires jQuery.
function scrollDown( options ) {
var settings, winHeight, elementTop;
// override settings by passing in the options object
settings = $.extend({
'offset': 70,
'selector': 'footer',
'speed': 'fast'
}, options);
winHeight = $(window).scrollTop() + $(window).height() + settings.offset;
elementTop = $(settings.selector).offset().top + $(settings.selector).height();
// scroll into view, if element is out of view
if ( elementTop > winHeight ) {
$('html, body').animate({
scrollTop: elementTop - settings.offset
}, settings.speed );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment