Skip to content

Instantly share code, notes, and snippets.

@zainaali
Created February 22, 2018 20:48
Show Gist options
  • Save zainaali/4b667f3eeaac8cfffd0737f323939930 to your computer and use it in GitHub Desktop.
Save zainaali/4b667f3eeaac8cfffd0737f323939930 to your computer and use it in GitHub Desktop.
Make scrolling sidebar along with page and stop at footer
(function($) {
//id of sidebar
var element = $('#sidebar')
originalY = $(element).offset().top;
// Space between element and top of screen (when scrolling)
var topMargin = 20;
// Should probably be set in CSS; but here just for emphasis
element.css('position', 'relative');
$(window).on('scroll', function(event) {
var scrollTop = $(window).scrollTop();
footer_view = scrollTop + $('#sidebar').height();
if(footer_view >= $('#footer').offset().top ){
element.stop(false, false).animate({
top: ($('#footer').offset().top - $('#sidebar').height() - 200)}, 300);
//element.stop()
}
else{
element.stop(false, false).animate({
top: scrollTop < originalY ? 0 : scrollTop - originalY + topMargin }, 300);
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment