Last active
December 17, 2015 06:29
-
-
Save brookemckim/5565986 to your computer and use it in GitHub Desktop.
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
$(document).scroll(function() { | |
$(".sticky").offset(function() { | |
var documentHeight = $(document).height(); | |
console.log(documentHeight); | |
$this = $(this); | |
var scrollTop = $(window).scrollTop(); | |
var offsetTop = $this.data("offset-top"); | |
if (scrollTop < 0) { | |
scrollTop = 0; | |
} | |
var newTop = offsetTop + scrollTop; | |
if (newTop < offsetTop) { | |
newTop = offsetTop; | |
} | |
// Prevents jitter at the bottom and document from | |
// infinitely expanding. | |
var maxTop = documentHeight - $this.height(); | |
if (newTop > maxTop) { | |
newTop = maxTop | |
} | |
// Prevents a bit of jitter since the current offset can be | |
// not precisely the initial offset. 338 Vs. 338.12931923 | |
var currentTop = $this.offset().top; | |
if ( Math.abs(currentTop - newTop) >= 1 ) { | |
return { top: newTop } | |
} else { | |
return {} | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
make sure you take console.log out - will break IE8 and lower