Animate/Shrink Navbar on Scroll Using Bootstrap
Creating a navbar that shrinks on scroll is a popular design element that enhances user experience by optimizing space on smaller screens. Here's how you can achieve this with Bootstrap:
Bootstrap 5
Bootstrap 5 introduced the sticky-top class, which enables you to create a static-to-fixed navbar effect. Simply add sticky-top to your navbar element, and it will stick to the top of the viewport when the page scrolls.
Bootstrap 4
Using Position: Sticky
Using IntersectionObserver API
Using jQuery
Example Using jQuery
<nav class="navbar navbar-inverse bg-inverse fixed-top"> <!-- your navbar markup --> </nav> <script> $(window).scroll(function() { if ($(document).scrollTop() > 100) { $('.navbar').addClass('sticky-top'); } else { $('.navbar').removeClass('sticky-top'); } }); </script>
Additional Notes
The above is the detailed content of How Can I Create a Navbar That Animates/Shrinks on Scroll Using Bootstrap?. For more information, please follow other related articles on the PHP Chinese website!