JSFiddle - React, Tailwind, and code Playground
HTML
<div id="profile-container">
<div id="fixed-header"></div>
<div class="container"></div>
<div class="sticky-header">This needs to be fixed when hits top of screen</div>
<div class="img">needs to be smooth</div>
<div class="img"></div>
<div class="img"></div>
</div>
CSS
#profile-container {
padding-top:52px;
/* Red bar height */
margin:0;
/* Clear default browser margin */
}
#profile-container.fixed {
padding-top:82px;
/* Red bar height + yellow bar height */
}
#profile-container {
height:1000px;
}
#fixed-header {
width:100%;
position:fixed;
height:50px;
border:1px solid black;
top:0px;
background-color:red;
}
.container {
height:300px;
width:100%;
background-color:blue;
}
.sticky-header {
width:700px;
height:50px;
background:orange;
}
.sticky-header {
height:30px;
width:100%;
background:yellow;
}
.fixed .sticky-header {
position: fixed;
top:52px;
margin-bottom:52px;
}
.img {
width:200px;
height:200px;
border:1px solid grey;
float:left;
}
JavaScript
var offset = $(".sticky-header").offset();
var sticky = document.getElementById("sticky-header")
var additionalPixels = 50;
$(window).scroll(function () {
if ($(window).scrollTop() > offset.top - additionalPixels) {
$('#profile-container').addClass('fixed');
} else {
$('#profile-container').removeClass('fixed');
}
});