JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div class="container">
<div id="box1" class="box"></div>
<div id="box2" class="box"></div>
<div id="box3" class="box"></div>
</div>
CSS
body {
margin: 50px;
}
.container {
width: 300px;
background: lightgrey;
padding: 50px;
}
.box {
display: block;
width: 100%;
height: 300px;
background: grey;
}
.box:not(:last-of-type) {
margin-bottom: 50px;
}
JavaScript
checkBoxes();
$(window).on('scroll', function() {
checkBoxes();
});
function checkBoxes() {
$('.box').each(function(i, v) {
if (_isInTheScreen($(window), $(v), -$(v).height())) {
$(v).css('background', 'red');
}
});
}
/*
* @param $ct : container - jQuery obj
* @param $el : element selected - jQuery obj
* @param optionOffset : offset
*/
function _isInTheScreen($ct,$el,optionOffset) {
var i = $ct,
g = $el,
e = i.scrollTop(),
d = i.scrollLeft(),
f = g.offset(),
l = f.top,
c = f.left,
j = true;
j = (e-100<=(l))&&((e+i.height()+optionOffset)>=((l))&&(d<=c)&&((d+i.width())>=c));
return j;
}