Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Header
<header>header</header>
<div id="sub_header" class="sub">sub</div>
<main>main</main>
$(window).on('scroll', function() {
var topInitial = $('#sub_header').offset().top;
var topPosition = 100 - $(window).scrollTop();
if (topPosition > 0) {
$('.sub').css('top', topPosition);
} else if(topPosition <= 0) {
$('.sub').css('top', 0);
}
});
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
* {
margin: 0;
padding: 0;
}
header {
height: 100px;
background: #ddf;
}
.sub {
display: block;
width: 100%;
height: 50px;
background: #ade;
position: fixed;
top: 100px;
}
main {
height: 2000px;
padding-top: 50px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment