Here's the pen...
http://codepen.io/jareko999/pen/bZXbWP
The 'if' portion of the jQuery works and adds the .fixed class to the #header when it's equal to or below 0, but it doesn't remove it when the headerTop is above 0, the 'else'. I can't figure out what's going on here. JS newb here, I'd like to understand how to get this to work. Thanks.
HTML
<div class="content">
<div id="header"></div>
</div>
CSS
body {
overflow-x: hidden;
margin: 0;
}
#header {
width: 100%;
height: 80px;
background: blue;
z-index: 1;
}
.content {
position: absolute;
top: calc(100% - 80px);
width: 100%;
height: 200vh;
background: linear-gradient(to bottom, red, yellow);
}
.fixed {
position: fixed;
top: 0;
width: 100%;
background: blue;
}
jQuery
$(window).scroll(function() {
var top = $('#header').offset().top;
var headerTop = (top - $(window).scrollTop());
if (headerTop <= 0) {
$('#header').addClass('fixed');
} else {
$('#header').removeClass('fixed');
}
});