Skip to content

Instantly share code, notes, and snippets.

@alepez
Created February 21, 2015 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alepez/9d0e28d1d0240dc4618f to your computer and use it in GitHub Desktop.
Save alepez/9d0e28d1d0240dc4618f to your computer and use it in GitHub Desktop.
sticky sidebar
function _makeSticky($sticky) {
var $parent = $sticky.parent();
var parentOffset = null;
var myOffset = null;
$(window).bind('scroll', function () {
var scroll = $(document.body).scrollTop();
if (myOffset === null) {
parentOffset = $parent.offset().top;
myOffset = $sticky.position().top;
}
if (scroll > parentOffset) {
$sticky.addClass("floating");
$sticky.css('top', scroll - parentOffset + myOffset);
} else {
$sticky.removeClass("floating");
$sticky.css('top', '');
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment