Created
February 21, 2015 14:35
-
-
Save alepez/9d0e28d1d0240dc4618f to your computer and use it in GitHub Desktop.
sticky sidebar
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 _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