JSFiddle - React, Tailwind, and code Playground
HTML
<div id="displayed-content" class="snap-content scrollable">
<div id="regularstuff" class="stuff">
<p>Scroll down, make sure the viewport is small to force scrollbars. When you get to the black header bar it should affix to the top and follow scrolling. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div id="incontentheader" class="stuff">
<h3>This should be come affixed to top of viewport when scrolled past.</h3>
</div>
<div id="moreregularstuff" class="stuff">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco...
CSS
.snap-content {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
width: auto;
height: auto;
z-index: 2;
overflow: auto;
-webkit-overflow-scrolling: touch;
-webkit-transform: translate3d(0, 0, 0);
-moz-transform: translate3d(0, 0, 0);
-ms-transform: translate3d(0, 0, 0);
-o-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
}
.scrollable{
overflow: auto;
-webkit-transition-property: top, bottom;
transition-property: top, bottom;
-webkit-transition-duration: .2s, .2s;
transition-duration: .2s, .2s;
-webkit-transition-timing-function: linear, linear;
transition-timing-function: linear, linear;
-webkit-overflow-scrolling: touch;
}
#incontentheader {
padding:10px;
background:#000;
color:#fff;
}
.stuff {
position:relative;
width:100%;
}
#footer {
background:#1e73b7;
padding:10px;
height:500px;
}
JavaScript
var fixed_from_top = function(the_el,limiter_el)
{
var fixedelement;
var fixedelementheight;
var limiterelement;
var normal_distance_from_top;
var limiter_distance_from_top;
this.configure = function()
{
this.fixedelement = the_el;
if(typeof(limiter_el)=='undefined') { this.limiterelement = false; }
else { this.limiterelement = limiter_el; }
this.getDistanceMeasure();
this.makeBindings();
}
this.getDistanceMeasure = function()
{
this.resetMe();
this.fixedelementheight = (this.fixedelement).height();
this.normal_distance_from_top = (this.fixedelement).offset().top + $("#displayed-content").scrollTop();
if(this.limiterelement)
{
this.limiter_distance_from_top = (this.limiterelement).offset().top + $("#displayed-content").scrollTop();
}
else
{
this.limiter_distance_from_top = 1000000000; //temp
}
}
this.resetMe = function()
{
(this.fixedelement).css({'top':'','z-index':''});
}
this.adjustMe = function(newpos)
{
(this.fixedelement).css({'top':newpos+'px','z-index':'100'});
}
this.checkScroll = function()
{
var curinstance = this;
var p = $("#displayed-content").scrollTop();
if(p>curinstance.normal_distance_from_top && (!curinstance.limiterelement || p<curinstance.limiter_distance_from_top-curinstance.fixedelementheight))
{
curinstance.adjustMe(p-curinstance.normal_distance_from_top);
}
else
{
curinstance.resetMe();
}
}
this.makeBindings = function()
{
var curinstance = this;
$("#displayed-content").on('scroll', function()
{
if(('ontouchstart' in window) || ('onmsgesturechange' in...