Skip to content

Instantly share code, notes, and snippets.

@gherkins
Forked from dejoe/scrollToViewPort.js
Created June 10, 2011 13:41
Show Gist options
  • Save gherkins/1018842 to your computer and use it in GitHub Desktop.
Save gherkins/1018842 to your computer and use it in GitHub Desktop.
Jquery Scroll To View Port
jQuery.fn.scrollToViewPort = function(options) {
var animTimeInterval = (typeof options.interval == "undefined")?"slow":animTimeInterval;
return this.each(function(){
var offSet = (typeof options.offset == "undefined")? $(this).offset().top : options.offset;
if( $(this).offset().top <= $(window).scrollTop() || ( $(this).offset().top + $(this).height() ) >= ( $(window).scrollTop() + $(window).height() ) ){
$('html,body').animate( { scrollTop: offSet }, animTimeInterval );
}
});
}
@gherkins
Copy link
Author

added check if element is already in viewport
added offset parameter to scroll to for example: $(this).scrollToViewPort( { offset : ( ( $(this).offset().top + my_custom_offset ) - $(window).height() ) } );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment