Skip to content

Instantly share code, notes, and snippets.

@corpsefilth
Created March 12, 2015 10:09
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 corpsefilth/f5505afc19a60d96f4bf to your computer and use it in GitHub Desktop.
Save corpsefilth/f5505afc19a60d96f4bf to your computer and use it in GitHub Desktop.
Create a clone of your menu -- sticky menu
// Create a clone of the menu, right next to original.
jQuery('.header')
.addClass('original')
.clone().insertAfter('.header')
.addClass('cloned')
.css('position','fixed')
.css('top','0')
.css('margin-top','0')
.css('z-index','500')
.removeClass('original')
.hide();
scrollIntervalID = setInterval(stickIt, 5); // refresh rate for function
function stickIt() {
var orgElementPos = jQuery('.original').offset();
var customWidth = jQuery(window).width(); // fullwidth continer
//console.log(customWidth);
orgElementTop = orgElementPos.top+500; // adjust offset when sticky menu is activated
if (jQuery(window).scrollTop() >= (orgElementTop)) {
// scrolled past the original position; now only show the cloned, sticky element.
// Cloned element should always have same left position and width as original element - or not.
orgElement = jQuery('.original');
coordsOrgElement = orgElement.offset();
leftOrgElement = 0;//coordsOrgElement.left;
widthOrgElement = customWidth;//orgElement.css('width');
jQuery('.cloned').css('left',leftOrgElement+'px').css('top',0).css('width',widthOrgElement+'px').show();
jQuery('.original').css('visibility','hidden');
} else {
// not scrolled past the menu; only show the original menu.
jQuery('.cloned').hide();
jQuery('.original').css('visibility','visible');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment