Skip to content

Instantly share code, notes, and snippets.

@manishsongirkar
Created December 5, 2014 12:07
Show Gist options
  • Save manishsongirkar/d9e5c371dd88740761d0 to your computer and use it in GitHub Desktop.
Save manishsongirkar/d9e5c371dd88740761d0 to your computer and use it in GitHub Desktop.
jQuery Sticky Navigation for my reference
/**
* Sticky Navigation
*
* @returns {undefined}
*/
$( function () {
// grab the initial top offset of the navigation
var sticky_navigation_offset_top = $( '#rtp-primary-menu' ).offset().top - 4;
// our function that decides weather the navigation bar should have "fixed" css position or not.
var sticky_navigation = function () {
var scroll_top = $( window ).scrollTop(); // our current vertical position from the top
// if we've scrolled more than the navigation, change its position to fixed to stick to top,
// otherwise change it back to relative
if ( scroll_top > sticky_navigation_offset_top ) {
if ( ! $( '#rtp-primary-menu' ).hasClass( 'rtp-fixed-nav' ) ) {
$( '#rtp-primary-menu' ).addClass( 'rtp-fixed-nav' );
}
} else {
if ( $( '#rtp-primary-menu' ).hasClass( 'rtp-fixed-nav' ) ) {
$( '#rtp-primary-menu' ).removeClass( 'rtp-fixed-nav' );
}
}
};
// run our function on load
sticky_navigation();
// and run it again every time you scroll
$( window ).scroll( function () {
sticky_navigation();
} );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment