Bootstrap 4 fixed navbar that hides until page scrolled up

John Avis by | March 8, 2018 | Bootstrap

Fixed navbars – that stay at the top of the screen at all times – are common and useful, but sometimes annoying as they can block the top part of the page (sometimes obscuring page anchors).
Fixed navbars – that stay at the top of the screen at all times – are common and useful, but sometimes annoying as they can block the top part of the page (sometimes obscuring page anchors).

I like the functionality that I have seen on some websites where the navbar will scroll away as you move down the page, but re-appears as soon as you start scrolling up the page.

To add this functionality to Bootstrap 4 I made the following changes to the Bootstrap 4 navbar.

CSS:

.navbar-outer {
position: relative;
height: 56px;
}
.absolute-top {
position: absolute;
top: 0;
right: 0;
left: 0;
z-index: 1030
}


jQuery (note two references to the navbar by id, in this example “navbar1”):

$(function () {
var lastPos = $(this).scrollTop();

setInterval(function () {
var thisPos = $(this).scrollTop();
if ($(this).scrollTop() > 1 && thisPos < (lastPos - 2)) {
$("#navbar1").removeClass("absolute-top").addClass("fixed-top");
} else {
if ($(this).scrollTop() <= 1 || thisPos > (lastPos + 2)) {
$("#navbar1").removeClass("fixed-top").addClass("absolute-top");
}
}
lastPos = thisPos;
}, 100);
});


For the HTML markup, several changes are required.

1) Add a div around the Bootstrap 4 navbar:

<div class="navbar-outer">
…navbar code here…
</div>


2) Give the navbar an id, the same as used in the jQuery code (“navbar1” in the sample).

3) Add the class “absolute-top” to the navbar, eg:

<div class="navbar-outer">
<nav id="navbar1" class="navbar navbar-expand-lg navbar-light bg-light absolute-top">
…navbar code here…


Please see the sample below for a demonstration of this.

Demo

Related Posts

Bootstrap Responsive Web Design

Responsive collapsing search filters in Bootstrap 4

by John Avis | March 26, 2018

In responsive design there are times when on a mobile screen you want to hide some options with a button to show them, but on a large screen always show these options.


Bootstrap Cool Websites

Bootstrap 4 beta Customiser

by John Avis | October 31, 2017

I love Bootstrap v4 and now that it is finally in Beta can start to use it in new projects. But I miss the online customizer that was available for Bootstrap v3.


Bootstrap ASP.NET Web Forms

ASP.NET Web Forms and Bootstrap 3 and 4 Modals

by John Avis | July 6, 2017

There are a few methods for hiding and showing Bootstrap 3.x and 4.x modals in an ASP.NET Web Forms application. This technique is pure ASP.NET (requires no client script) and allows you to show and hide modals by changing the Visible property of a containing PlaceHolder or Panel.

Comments

There are no comments yet. Be the first to leave a comment!

Leave a Comment
Tags
ASP.NET Html Forms ASP.NET MVC ASP.NET Web Forms ASP.NET Web Pages Bootstrap C# Classic ASP Cool Websites Databases eBay and PayPal Electrical Repairs General Hardware HTML/CSS Jquery/Javascript Media Center Mobile Phones Responsive Web Design SEO and Social Networking Web Design Web Development Web Security web+db Website Hosting Windows XP

About me

...mostly about web development and programming, with a little bit of anything else related to the Internet, computers and technology.

Subscribe

Get the latest posts delivered to your inbox.