Created
February 22, 2018 20:48
-
-
Save zainaali/4b667f3eeaac8cfffd0737f323939930 to your computer and use it in GitHub Desktop.
Make scrolling sidebar along with page and stop at footer
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
(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