-
-
Save gherkins/1018842 to your computer and use it in GitHub Desktop.
Jquery Scroll To View Port
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
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 ); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
added check if element is already in viewport$(this).scrollToViewPort( { offset : ( ( $ (this).offset().top + my_custom_offset ) - $(window).height() ) } );
added offset parameter to scroll to for example: