mikeoberdick

Sticky Header on Scroll

Apr 9th, 2016
1,119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 1.15 KB | None | 0 0
  1. <script>
  2. // Create a clone of the menu, right next to original
  3. $('.site-header').addClass('original').clone().insertAfter('.site-header').addClass('cloned').css('position','fixed').css('top','0').css('margin-top','0').css('z-index','500').removeClass('original').hide();
  4.  
  5. scrollIntervalID = setInterval(stickIt, 10);
  6.  
  7. function stickIt() {
  8.  
  9.  var orgElementPos = $('.original').offset();
  10.   orgElementTop = orgElementPos.top;              
  11.  
  12.   if ($(window).scrollTop() >= (orgElementTop)  ) {
  13.     // scrolled past the original position; now only show the cloned, sticky element.
  14.  
  15.     // Cloned element should always have same left position and width as original element.    
  16.     orgElement = $('.original');
  17.     coordsOrgElement = orgElement.offset();
  18.     leftOrgElement = coordsOrgElement.left;  
  19.     widthOrgElement = orgElement.css('width');
  20.     $('.cloned').css('left',leftOrgElement+'px').css('top',0).css('width',widthOrgElement).show();
  21.     $('.original').css('visibility','hidden');
  22.   } else {
  23.     // not scrolled past the menu; only show the original menu.
  24.     $('.cloned').hide();
  25.     $('.original').css('visibility','visible');
  26.   }
  27. }
  28. </script>
Add Comment
Please, Sign In to add comment