Skip to content

Instantly share code, notes, and snippets.

@jaggy
Last active August 29, 2015 13:55
Show Gist options
  • Save jaggy/8774505 to your computer and use it in GitHub Desktop.
Save jaggy/8774505 to your computer and use it in GitHub Desktop.
Just a snippet to make a bar fixed with a given breakpoint and lower element to adjust
$.fn.sticky = function( options ){
console.error( options );
var element = $( this );
var breakpoint = options.breakpoint;
var container = $( options.container );
var detect_breakpoint = function() {
var scroll = $( document ).scrollTop();
// get the height
var padding = element.outerHeight();
// check to see if scroll has exceeded the navigation height
var has_exceeded = ( scroll > breakpoint );
if( has_exceeded ) {
// set the element position as fixed
element.css( 'position', 'fixed' );
// set the position on top
element.css( 'top', 0 );
// add a `padding-top` to the container with the element's height
container.css( 'padding-top', padding );
} else {
// set the element position to static
element.css( 'position', 'static' );
// remove the container `padding-top`
container.css( 'padding-top', 0 );
}
}
$( window ).scroll( detect_breakpoint );
return $( this );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment