Created
May 25, 2010 15:28
-
-
Save michaeldwan/413265 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
(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