Skip to content

Instantly share code, notes, and snippets.

@brookemckim
Last active December 17, 2015 06:29
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 brookemckim/5565986 to your computer and use it in GitHub Desktop.
Save brookemckim/5565986 to your computer and use it in GitHub Desktop.
$(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 {}
}
});
});
@bkosborne
Copy link

make sure you take console.log out - will break IE8 and lower

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment