Advertisement
Guest User

HTML5 UP! - Arcana sticky navigation

a guest
Aug 28th, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Sticky menu
  2. $( window ).load(function() {
  3.  
  4.     // grab the initial top offset of the navigation
  5.     var topElement = $('#header h1');
  6.     var el = $('#nav');
  7.     var sticky_navigation_offset_top = 164; //default for 1080p screens
  8.          
  9.     // our function that decides weather the navigation bar should have "fixed" css position or not.
  10.     var sticky_navigation = function(){
  11.         var scroll_top = $(window).scrollTop(); // our current vertical position from the top
  12.          
  13.         // if we've scrolled more than the navigation, change its position to fixed to stick to top,
  14.         // otherwise change it back to relative
  15.         if (scroll_top > sticky_navigation_offset_top) {
  16.             el.css({ 'position': 'fixed', 'top':0, 'left':0, 'zIndex':1000 });
  17.         } else {
  18.             el.css({ 'position': 'relative' });
  19.         }  
  20.     };
  21.      
  22.     //Set top offset for navbar
  23.     var setTopOffset = function(){
  24.         //our offset is the bottom of the top element.
  25.         sticky_navigation_offset_top = topElement.offset().top + topElement.outerHeight();
  26.     };
  27.      
  28.     // run our function on load
  29.     sticky_navigation();
  30.     setTopOffset();
  31.      
  32.     // and run it again every time you scroll
  33.     $(window).scroll(function() {
  34.          sticky_navigation();
  35.     });
  36.    
  37.     //On resize the css might have changed the navigation panel so a new offset should be calculated
  38.     $( window ).resize(function() {
  39.         setTopOffset();
  40.     });
  41.  
  42. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement