JSFiddle - React, Tailwind, and code Playground
HTML
<div id="wrapper">
scroll down to see the div
</div>
<div id="tester"></div>
CSS
body, html {
height: 100%;
}
#wrapper {
height: 400%;
background: none repeat scroll 0 0 #c99 ;
text-align:center;
padding-top:20px;
font-size:22px;
}
#tester {
padding-top:1200px;
height: 600px;
background: none repeat scroll 0 0 #fff;
border: 5px solid black;
}
JavaScript
$(window).scroll(function() {
if (checkVisible($('#tester'))) {
alert("Visible!!! Paste your code in this if function!")
} else {
// do nothing
}
});
function checkVisible( elm, eval ) {
eval = eval || "visible";
var vpH = $(window).height(), // Viewport Height
st = $(window).scrollTop(), // Scroll Top
y = $(elm).offset().top,
elementHeight = $(elm).height();
if (eval == "visible") return ((y < (vpH + st)) && (y > (st - elementHeight)));
if (eval == "above") return ((y < (vpH + st)));
}