I've searched a whole heap for this but cant seem to find a working solution. Basically I've got my NavBar perfectly how I want it. I now want to shrink the NavBar when my page scrolls down to make it more slim using a nice smooth transition (animation).
This is my HTML markup for my current NavBar:
<header>
<nav class="navbar fixed-top navbar-toggleable-md navbar-inverse bg-inverse">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#"><img class="animated bounceIn" src="img/logo-light.png" alt="I Web You – Perth Website Branding"></a>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Portfolio</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Blog</a>
</li>
</ul>
</div>
</nav>
</header>
Any ideas how I can achieve this? I've done a lot of searching but most solutions were for Bootstrap 3.
Cheers
Both of the above answers looked long and complicated, surely (I thought) there must be a really sleek way to do this now in Bootstrap 4.1 - it's 2018! Well yeh - nah....There are some interesting answers around the net, if you have a look at some of the templates at startbootstrap.com for example.
I didn't see anything vastly different from those above...
Add or remove a class on scroll/ page load...
Then style based on that appended class...
Updated 2018
Most of the shrink on scroll Navbar implementations for Bootstrap 3.x are done using the Affix component to change the style of the navbar at a specific scroll position.
However, Affix has been dropped from Bootstrap 4..
To create a similar Navbar effect in Bootstrap 4, you can use
position: sticky
(not supported in all browsers) by adding thesticky-top
class to the Navbar. This works to "stick" the Navbar when it reaches the top, but doesn't trigger an event to indicate when the it's "stuck". Therefore, JS is needed to change the Navbar style when it's "stuck".One JS method supported in modern browsers is
IntersectionObserver
. Use it to "watch" when a hidden trigger element above the Navbar reaches the viewport (whenstuck
is applied to thehtml
element).Sticky Top Navbar - IntersectionObserver Demo
You could also jQuery plugin such as ScrollPos-Styler, or write your own jQuery to control the navbar styles on scroll...
How it works:
A fixed-top Navbar with
data-toggle="affix"
:jQuery to watch scroll position and conditionally toggle the
.affix
class:CSS to manipulate the affix style (padding/height, color, etc..):
Note: As of Bootstrap 4.0.0, the Navbar has changed slightly as
navbar-toggleable-*
has change tonavbar-expand-
Sticky Top Navbar - jQuery Demo
Finally, you can use a simple jQuery $(window).scroll() function if you know the exact position of when the Navbar needs to stick...
https://www.codeply.com/go/62Roy6RDOa
More Bootstrap 4 change Navbar style on scroll examples:
simply sticky after scroll (4.0.0)
shrink height (4.0.0)
shrink height (alpha-6)
transparent over background
change color after scroll
change navbar bg color with scrollspy sections
Related questions:
Fixing navbar to top on scroll
Affix is not working in Bootstrap 4 (alpha)
Use this code.It's simple and easy. I hope you would get a understanding. I have made a class shrink which it would add to the navbar once it crosses 50 pixels. And rest is all css, which I have put in the Shrink class.