I am attempting to create a header with two positions - one absolute and one fixed. I want the header to smoothly slide in as you scroll down, and then slide out when you scroll back to the top. However, I am having trouble getting it to slide; instead, it just appears and disappears abruptly while scrolling.
(function($) {
$(document).ready(function(){
$(window).scroll(function(){
if ($(this).scrollTop() > 300) {
$('.header').addClass('fixed');
} else {
$('.header').removeClass('fixed');
}
});
});
})(jQuery);
.header {
position: absolute;
width:100%;
height:86px;
background: red;
color: #fff;
}
.header.fixed {
width:100%;
height:66px;
position:fixed;
top:0px;
background:#000;
color: #fff;
-moz-transform: translateY(-130px);
-ms-transform: translateY(-130px);
-webkit-transform: translateY(-130px);
transform: translateY(-130px);
transition: transform .5s ease;
}
.header.fixed {
-moz-transform: translateY(0);
-ms-transform: translateY(0);
-webkit-transform: translateY(0);
transform: translateY(0);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="header">
<span>My Div</span>
</div>
<div style="height: 2000px; background-color: grey;">Content</div>