Use the Bootstrap Affix component to watch the scroll position instead of extra jQuery/JavaScript. Just define the .affix-top
and .affix
CSS for the navbar.
/* navbar style once affixed to the page */
.affix {
background-color: black;
}
@media (min-width:768px) {
.affix-top {
/* navbar style at top */
background-color:transparent;
border-color:transparent;
padding: 15px;
}
}
and set the position you want the navbar to change style (ie; 400px from the top)
<div class="navbar navbar-inverse navbar-fixed-top" data-spy="affix" data-offset-top="400">
Working Demo:
http://www.codeply.com/go/xp2fY6sVHP
For Bootstrap 4, see: Animate/Shrink NavBar on scroll using Bootstrap 4
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…