Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rleaf/60e2f7b81dc8898e25de43362cb1b68d to your computer and use it in GitHub Desktop.
Save rleaf/60e2f7b81dc8898e25de43362cb1b68d to your computer and use it in GitHub Desktop.
CSS3 on scroll fade in navigation background

CSS3 on scroll fade in navigation background

I had a comment on a previous pen (http://codepen.io/willpaige/pen/optzh) that they had used exactly the same technique, but using CSS instead. So I decided to give it a go!

Its a useful animation/style when using a fixed nav which needs more definition further down the page.

A Pen by Will Paige on CodePen.

License.

<div id="top" class="container">
<div class="fixed">
<div class="bg transition"></div>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
</ul>
</div>
<a href="#bottom" class="scroll down">SCROLL DOWN</a>
<a href="#top" id="bottom" class="scroll up">SCROLL UP</a>
</div>
$(window).scroll(function() {
// 100 = The point you would like to fade the nav in.
if ($(window).scrollTop() > 100 ){
$('.bg').addClass('show');
} else {
$('.bg').removeClass('show');
};
});
$('.scroll').on('click', function(e){
e.preventDefault()
$('html, body').animate({
scrollTop : $(this.hash).offset().top
}, 1500);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
.container {
width: 100%;
height: 2000px;
position: relative;
font-family: 'Trebuchet Ms';
}
.bg {
background: #000;
width: 100%;
height: 100px;
opacity: 0;
}
.show {
opacity: 0.4;
}
.transition {
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
}
.fixed {
position: fixed;
top: 0;
left: 0;
width: 100%;
z-index: 1;
}
ul {
height: 200px;
margin: -70px auto 0 auto;
width: 500px;
}
li {
float: left;
list-style: none;
margin: 10px 20px;
text-transform: uppercase;
letter-spacing: 4px;
color: #000;
}
a {
text-align: center;
font-size: 50px;
color: #bdbdbd;
font-weight: bold;
text-decoration: none;
letter-spacing: 5px;
text-shadow: 1px 1px 1px #949494;
position: relative;
z-index: 1;
margin: 0 auto;
display: block;
}
a:hover {
color: #a6a6a6;
text-shadow: 1px 1px 1px #C9C9C9;
}
.down {
top: 150px;
}
.up {
top: 1800px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment