Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shrink navbar while scrolling down using Bootstrap 3

I am using Bootstrap 3 for developing a website. I need to create a navbar which should shrink as the user scrolls down.

Something like - http://osticket.com

How can I create this? I am using Bootstrap's fixed-top example as a starting point - http://getbootstrap.com/examples/navbar-fixed-top/

I need to put a logo onto the left instead of the text and it should reduce its size as the user scrolls down.

like image 965
user3301160 Avatar asked May 08 '26 16:05

user3301160


2 Answers

Good example of shrinking when scrolling has been documented here: http://www.bootply.com/109943

$(window).scroll(function() {
  if ($(document).scrollTop() > 50) {
    $('nav').addClass('shrink');
  } else {
    $('nav').removeClass('shrink');
  }
});
like image 164
Yabs Avatar answered May 11 '26 07:05

Yabs


You can simply do it the same way they did it. If you look at the page source, they are simply adding a class to the body element when the page is scrolled. This class is overwriting the display format of the header.

To detect if the page has been scrolled down, they are using a javascript function with events listeners. You can look at the function source code here:

http://osticket.com/sites/all/themes/Porto/js/sticky.js

like image 43
DjDCH Avatar answered May 11 '26 07:05

DjDCH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!