Skip to content

Instantly share code, notes, and snippets.

@michaeldwan
Created May 25, 2010 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save michaeldwan/413265 to your computer and use it in GitHub Desktop.
Save michaeldwan/413265 to your computer and use it in GitHub Desktop.
(function($){
$.fn.sticky = function () {
var toolbar = $(this);
var wrapper = $(this).wrap($('<div />', { id : toolbar.attr('id') + '-wrapper'})).parent();
$(this).width(wrapper.width());
wrapper.height($(this).outerHeight());
$(window).bind('scroll resize', function () {
toolbar.toggleClass('follow', !$.fn.sticky.isScrolledIntoView(wrapper));
});
$(window).resize();
};
$.fn.extend($.fn.sticky, {
isScrolledIntoView: function (elem, bottom) {
var docViewTop = $(window).scrollTop();
var docViewBottom = docViewTop + $(window).height();
var elemTop = elem.offset().top;
var elemBottom = elemTop + elem.height();
return ((elemBottom >= docViewTop) && (elemBottom <= docViewBottom));
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment