Skip to content

Instantly share code, notes, and snippets.

@ntulip
Forked from michaeldwan/jquery.stickyToolbar.js
Created May 25, 2010 15:58
Show Gist options
  • Save ntulip/413311 to your computer and use it in GitHub Desktop.
Save ntulip/413311 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