Skip to content

Instantly share code, notes, and snippets.

@juanbrujo
Last active August 29, 2015 14:04
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 juanbrujo/a76d00fec8561cb0ad76 to your computer and use it in GitHub Desktop.
Save juanbrujo/a76d00fec8561cb0ad76 to your computer and use it in GitHub Desktop.
stickyNav: easy jQuery horizontal sticky function
/**
* jquery.stickyNav.js
**/
var stickyNav = function(elem,offset){
$(window).scroll(function(){
if( $(window).scrollTop() > offset ) {
elem.css({position: 'fixed', top: 0, 'z-index': 99});
} else {
elem.css({position: 'static', top: 0});
}
});
}
// END
/**
* Use:
* stickyNav(elem,offset):
*
* elem: jQuery object that get glued to top viewport
* offset: distance to top
*
**/
var stickyElem = $('#sticky-nav'),
stikyOffset = stickyElem.offset().top;
stickyNav(stickyElem,stikyOffset);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment