Skip to content

Instantly share code, notes, and snippets.

@MSerj
Created October 30, 2017 19:27
Show Gist options
  • Save MSerj/a46501705b1c9a58b5d973cef4ba04d2 to your computer and use it in GitHub Desktop.
Save MSerj/a46501705b1c9a58b5d973cef4ba04d2 to your computer and use it in GitHub Desktop.
Sticky menu on scroll
// Create a clone of the menu, right next to original.
$('.top_fixed_menu').addClass('original').clone().insertAfter('.top_fixed_menu').addClass('cloned').css('position','fixed').css('top','0').css('margin-top','0').css('z-index','500').removeClass('original').hide();
scrollIntervalID = setInterval(stickIt, 10);
function stickIt() {
var orgElementPos = $('.original').offset();
orgElementTop = orgElementPos.top;
if ($(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.
orgElement = $('.original');
coordsOrgElement = orgElement.offset();
leftOrgElement = coordsOrgElement.left;
widthOrgElement = orgElement.css('width');
$('.cloned').css('left',leftOrgElement+'px').css('top',0).css('width',widthOrgElement).show();
$('.original').css('visibility','hidden');
} else {
// not scrolled past the menu; only show the original menu.
$('.cloned').hide();
$('.original').css('visibility','visible');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment