Advertisement
Guest User

Code to switch stationary navbar to fixed - Bootstrap

a guest
Feb 29th, 2012
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  $(window).load(function() {
  2.     // fix sub nav on scroll
  3.     var $win = $(window)
  4.       , $nav = $('.navbar')
  5.       , $nC = $('#navCont')
  6.       , navTop = $('.navbar').length && $('.navbar').offset().top + 20
  7.       , isFixed = 0
  8.      
  9.     $nav.addClass('auto-size');
  10.  
  11.     processScroll()
  12.    
  13.     $nC.addClass('auto-size')
  14.     $win.on('scroll', processScroll)
  15.  
  16.     function processScroll() {
  17.       var i, scrollTop = $win.scrollTop()
  18.       if (scrollTop >= navTop && !isFixed) {
  19.         isFixed = 1
  20.         $nC.removeClass('auto-size')
  21.         $nav.addClass('navbar-fixed-top');
  22.       } else if (scrollTop <= navTop && isFixed) {
  23.         isFixed = 0
  24.         $nC.addClass('auto-size')
  25.         $nav.removeClass('navbar-fixed-top');
  26.       }
  27.     }
  28. });
  29.  
  30. <!-- Put this in your stylesheet -->
  31. .auto-size { width: auto; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement