I'm fairly new to JQuery and I am trying to build a navigation that hides when scrolling down and reappears with a black background when scrolling up.
I have achieved this so far, but now I want the background color of my navigation to change from black back to transparent when scrolling back to the very top of the page.
Here is my progress - http://dwaynecrawford.com/testblog/
I want to achieve an effect identical to this navigation - http://www.undsgn.com/uncode/when-you-are-alone/
Here is my code:
/* Hide Navbar */
// Hide Header on on scroll down
var didScroll;
var lastScrollTop = 1;
var delta = 5;
var navbarHeight = $('nav').outerHeight();
$(window).scroll(function(event) {
didScroll = true;
});
setInterval(function() {
if (didScroll) {
hasScrolled();
didScroll = false;
}
}, 250);
function hasScrolled() {
var st = $(this).scrollTop();
// Make sure they scroll more than delta
if (Math.abs(lastScrollTop - st) <= delta)
return;
// If they scrolled down and are past the navbar, add class .nav-up.
// This is necessary so you never see what is "behind" the navbar.
if (st > lastScrollTop && st > navbarHeight) {
// Scroll Down
$('nav').removeClass('nav-down').addClass('nav-up');
} else {
// Scroll Up
if (st + $(window).height() < $(document).height()) {
$('nav').removeClass('nav-up').addClass('nav-down, nav-blk');
}
}
lastScrollTop = st;
}
body{
background-color: #ababab;
}
nav {
width: 100%;
margin: 0 auto;
/*text-align: center;
display: inline;*/
display: table;
vertical-align: middle;
text-align: center;
position: fixed;
height: auto;
opacity: 1.8;
word-spacing: 20px;
/*border-bottom: #5c5c5c solid 1px;*/
height: 5vh;
}
nav a {
text-decoration: none;
color: #fff;
}
nav a:hover {
color: #9f9f9f;
font-weight: 700;
}
nav a:visited {
color: #fff;
}
.nav {
position: fixed;
top: 0px;
color: #fff;
padding-top: 15px;
padding-bottom: 0px;
text-transform: uppercase;
font-size: .75em;
transition: top 0.2s ease-in-out;
z-index: 1;
}
.nav-up {
top: -10vh;
}
.nav-blk {
background-color: #000;
opacity: .7;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav class="nav hideme nav-down">
<article class="navlogo">Navigation</article>
<article class="navigation">
<ul>
<li><a href="#head">Home</a>
</li>
<li><a href="#about">About</a>
</li>
<li><a href="#work">Work</a>
</li>
<li><a href="#team">Blog</a>
</li>
<li><a href="#contact">Contact</a>
</li>
</ul>
</article>
</nav>