Skip to content

Instantly share code, notes, and snippets.

@itsjavi
Created March 21, 2014 10:02
Show Gist options
  • Save itsjavi/9683028 to your computer and use it in GitHub Desktop.
Save itsjavi/9683028 to your computer and use it in GitHub Desktop.
Soft scroll to an element
$.fn.skrollto = function(speed, easing) {
speed = speed ||  1000;
easing = easing || 'swing';
return $(this).each(function(i, el){
//calculate destination place
var dest = 0;
var el = $(this);
if (el.is(':hidden')) {
el.show();
}
if (el.offset().top > $(document).height() - $(window).height()) {
dest = $(document).height() - $(window).height();
} else {
dest = el.offset().top;
}
//go to destination
$('html,body').animate({
scrollTop: dest
}, speed, easing);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment