I am currently using bootstrap to design a website, and I am attempting to create a div that would initially appear above the navbar on an unscrolled webpage. However, once the user starts scrolling, I want this div to disappear and have the navbar fill its position.
While my current setup kind of works, I believe there must be a more efficient way to achieve this effect. Here is the code snippet I am using:
HTML
<div class="container">
<div class="row">
<div class="hidden-xs col-md-offset-8 col-sm-offset-8" style="padding:2px 0 3px 0;">
<span class="btcall">Call for a free quote call - <span class="badge"><i class="glyphicon glyphicon-earphone"></i> 1300 123 456</span>
</div>
</div>
</div>
<nav class="navbar navbar-default navbar-fixed-top topnav" role="navigation">
etc etc
JavaScript
$(window).scroll(function() {
if ($(document).scrollTop() > 50) {
$('nav').addClass('shrink');
} else {
$('nav').removeClass('shrink');
}
});
CSS
.navbar-fixed-top {
top:25px;
}
nav a {
padding-top: 20px !important;
padding-bottom: 20px !important;
font-size: 16px;
}
nav .navbar-toggle {
margin: 13px 15px 13px 0;
}
.navbar-brand {
font-size: 30px !important;
margin-top:-15px;
}
.navbar-fixed-top.shrink {
top:0px;
}
nav.navbar.shrink {
min-height: 35px;
}
nav.shrink a {
padding-top: 15px !important;
padding-bottom: 10px !important;
font-size: 14px;
}
nav.shrink .navbar-brand {
font-size: 20px !important;
margin-top:0;
}
Although it somewhat does the job, I know there must be a better way to accomplish this. Ideally, I would like the element to be fixed to the navigation bar and hidden upon scrolling, rather than being locked in place and only revealed when the browser is scrolled all the way up to the top.