Skip to content

Instantly share code, notes, and snippets.

@jakebellacera
Created June 21, 2011 07:42
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 jakebellacera/1037413 to your computer and use it in GitHub Desktop.
Save jakebellacera/1037413 to your computer and use it in GitHub Desktop.
"Sticky" elements -- scrolls with the content when the window begins to hide it
// "Stuck" menu listing header
var Sticky = function( $obj, opts ){
$(window).scroll(
function(e){
Sticky.onScroll(e, $obj, opts );
});
}
Sticky.onScroll = function( e, $o, opts ){
var iScrollTop = $(window).scrollTop();
var sClass = "sticky";
//set original data
if( !$o.data(sClass) ){
$o.data(sClass, {css:{position:$o.css('position'),top:$o.css('top')}, offset:$o.offset()} );
}
var oOrig = $o.data(sClass);
var bIsSticky = $o.hasClass(sClass);
if( iScrollTop > oOrig.offset.top && !bIsSticky ){
if( $('body').hasClass('admin-bar')) {
$o.css({position:'fixed',top:38}).addClass(sClass);
} else {
$o.css({position:'fixed',top:10}).addClass(sClass);
}
}else if(iScrollTop < oOrig.offset.top && bIsSticky){
$o.css(oOrig.css).removeClass(sClass);
}
}
Sticky( $('#menuhead') );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment