How to fixed bootstrap navbar at top on scroll
How to fixed bootstrap navbar at top on scroll
In this session, we’ll try our hand at solving the "How to fixed bootstrap navbar at top on scroll" puzzle by using the computer language.
You can fix the bootstrap navbar at the top on scroll. Bootstrap has a property fixed to fix the navbar at the top of the screen. You can add and remove this class using javascript-
Fixed bootstrap navbar at top on scroll using javascript
<!-- Bootstrap 4 Navbar (HTML) --> <div id="navbar_top" class="container-fluied bg-light shadow-sm"> <div class="container"> <div class="row"> <div class="col-md-12"> <nav class="navbar navbar-expand-lg navbar-light bg-light"> <button class="navbar-toggler" 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> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="navbar-nav mr-auto"> <li class="nav-item"> <a class="nav-link" href="{{ route('allGenerators') }}#html"> HTML </a> </li> <li class="nav-item"> <a class="nav-link" href="{{ route('allGenerators') }}#bootstrap"> Bootstrap </a> </li> <li class="nav-item"> <a class="nav-link" href="{{ route('allGenerators') }}#MySQL"> MySQL </a> </li> <li class="nav-item"> <a class="nav-link" href="{{ route('allGenerators') }}#laravel"> Laravel </a> </li> <li class="nav-item"> <a class="nav-link" href="{{ route('allGenerators') }}#codeigniter"> Codeigniter </a> </li> <li class="nav-item"> <a class="nav-link" href="{{ route('allGenerators') }}#htaccess"> htaccess </a> </li> <li class="nav-item"> <a class="nav-link" href="{{ route('allGenerators') }}#cryptography"> Cryptography & Security </a> </li> <li class="nav-item"> <a class="nav-link" href="{{ route('allGenerators') }}#dummydata"> Dummy data </a> </li> </ul> </div> </nav> </div> </div> </div> </div> <!-- Javascript --> <script> document.addEventListener("DOMContentLoaded", function(){ window.addEventListener('scroll', function() { if (window.scrollY > 50) { document.getElementById('navbar_top').classList.add('fixed-top'); // add padding top to show content behind navbar navbar_height = document.querySelector('.navbar').offsetHeight; document.body.style.paddingTop = navbar_height + 'px'; } else { document.getElementById('navbar_top').classList.remove('fixed-top'); // remove padding top from body document.body.style.paddingTop = '0'; } }); }); </script>
You can fixed bootstrap navbar on scroll in html using bootstrap navbar and javascript
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Javascript
- How to preview video before uploading in javascript
- How can I add a key/value pair to a JavaScript object
- Input type file styling
- Remove property from object in javascript
- Javascript get data-id attribute value
- Javascript to print Fibonacci series upto 15 Observations
- Get hostname from URL in javascript
- How to validate empty space in textbox in Javascript
- Uncaught TypeError: Cannot read property 'addEventListener' of undefined
- Uncaught TypeError: Assignment to constant variable
- How to pass array as arguments to function in javascript
- Apexcharts example code
- How to switch between Dark Mode and Light Mode based on cookie in Javascript
- Custom slider js
- Uncaught SyntaxError: Identifier 'CODE' has already been declared
- How to get data from localstorage in javascript
- Ezsnippets (76) challenge - javascript - youtube shorts
- Remove hostname from URL in javascript
- ReferenceError: $ is not defined
- How to show and hide placeholder text
- Apex bar chart - fill bar color with gradient on hover
- JavaScript test() Method
- How to push string in an array in Javascript
- How to get file extension using javascript/jquery
- Apex chart bar chart