Created
May 19, 2011 04:42
-
-
Save jakebellacera/980202 to your computer and use it in GitHub Desktop.
When an element scrolls out of the window, it "sticks" in it's position.
This file contains 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
// "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