Makes a transparent navbar become solid once user scrolls past a certain point
Created
June 6, 2020 18:56
-
-
Save enzienza/f5dfe69b9ad13bc0c0d7a4c74c1cf554 to your computer and use it in GitHub Desktop.
Transparent to Solid navbar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<body> | |
<nav class="navbar navbar-default navbar-fixed-top"> | |
<div class="container"> | |
<div class="navbar-header"> | |
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> | |
<span class="sr-only">Toggle navigation</span> | |
<span class="icon-bar"></span> | |
<span class="icon-bar"></span> | |
<span class="icon-bar"></span> | |
</button> | |
<a class="navbar-brand" href="#">Transparent to Solid Nav</a> | |
</div> | |
<div id="navbar" class="collapse navbar-collapse"> | |
<ul class="nav navbar-nav navbar-right"> | |
<li><a href="#">About</a></li> | |
<li><a href="#">Link</a></li> | |
<li><a href="#">Another Link</a></li> | |
</ul> | |
</div><!--/.nav-collapse --> | |
</div> | |
</nav> | |
<div class="image"> | |
<h1 class="heading">Main Heading</h1> | |
<p><button class="btn btn-large">Big Button</button></p> | |
</div> | |
<div class="about"> | |
<h2>Main Description</h2> | |
<p>Roll on the floor purring your whiskers off intrigued by the shower burrow under covers, and play time, rub face on everything, intently sniff hand, or pelt around the house and up and down stairs chasing phantoms. Attack feet. Damn that dog shake treat bag under the bed drink water out of the faucet for lick butt love to play with owner's hair tie. Swat at dog give attitude.</p> | |
</div> | |
</body> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
// Transition effect for navbar | |
$(window).scroll(function() { | |
// checks if window is scrolled more than 500px, adds/removes solid class | |
if($(this).scrollTop() > 500) { | |
$('.navbar').addClass('solid'); | |
} else { | |
$('.navbar').removeClass('solid'); | |
} | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.image { | |
background-color: #490D40; | |
background: url(https://sonorangirl.github.io/Earth-Quotes/img/google-earth-view-South-Iceland3.jpg) no-repeat center center; | |
background-size: cover; | |
height: 570px; | |
margin-top: -20px; | |
.heading { | |
color: white; | |
text-align: center; | |
padding-top: 200px; | |
} | |
p { | |
text-align: center; | |
padding: 20px 60px; | |
.btn { | |
background-color: transparent; | |
color: white; | |
border: 1px solid white; | |
padding: 10px; | |
font-size: 1.2em; | |
&:hover { | |
color: #C57ED3; | |
border-color: #C57ED3; | |
} | |
} | |
} | |
} | |
.navbar { | |
height: 80px; | |
background-color: transparent; | |
border: none; | |
color: white; | |
z-index: 100; | |
transition: background-color 1s ease 0s; | |
} | |
.navbar-default { | |
.navbar-brand { | |
margin-top: 10px; | |
color: white; | |
&:hover { | |
color: #C57ED3; | |
border: 1px solid #C57ED3; | |
} | |
} | |
.navbar-nav { | |
> li { | |
> a { | |
color: white; | |
margin: 10px 5px 5px 5px; | |
} | |
> a:hover, > a:active, > a:focus { | |
color: #C57ED3; | |
border: 1px solid #C57ED3; | |
} | |
> a:visited { | |
color: #C57ED3; | |
text-decoration: none; | |
} | |
} //end li | |
> .active { | |
> a, > a:hover, > a:focus { | |
background-color: transparent; | |
color: #C57ED3; | |
} | |
} | |
} //end .navbar-nav | |
//Collapsed styles | |
.navbar-toggle { | |
border-color: #C57ED3; | |
&:hover, &:focus { | |
background-color: #490D40; | |
} | |
.icon-bar { | |
color: #C57ED3; | |
background-color: #C57ED3; | |
} | |
} | |
.navbar-collapse.collapse.in ul { | |
background-color: #490D40; | |
} | |
} //end .navbar-default | |
/* Solid class attached on scroll past first section */ | |
.navbar.solid { | |
background-color: #490D40; | |
transition: background-color 1s ease 0s; | |
box-shadow: 0 0 4px grey; | |
.navbar-brand { | |
color: #C57ED3; | |
transition: color 1s ease 0s; | |
} | |
.navbar-nav { | |
> li { | |
> a { | |
color: #C57ED3; | |
transition: color 1s ease 0s; | |
} | |
} //end li | |
} //end .navbar-nav | |
} | |
.about { | |
background-color: #C57ED3; | |
color: #490D40; | |
height: 600px; | |
text-align: center; | |
margin-top: -20px; | |
h2 { | |
padding-top: 220px; | |
} | |
p { | |
padding: 20px 80px; | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" /> | |
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment