JSFiddle - React, Tailwind, and code Playground
HTML
There is an orange div lower on the screen. Click any box to alert if the orange box is within the visible viewport.
<div class="box orange" id="boxorange"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
CSS
.box {
width: 200px;
height: 200px;
margin-bottom: 10px;
background: blue;
}
.orange {
background: orange;
}
JavaScript
$.fn.isOnScreen = function(){
var win = $(window);
var viewport = {
top : win.scrollTop(),
left : win.scrollLeft()
};
viewport.right = viewport.left + win.width();
viewport.bottom = viewport.top + win.height();
var bounds = this.offset();
bounds.right = bounds.left + this.outerWidth();
bounds.bottom = bounds.top + this.outerHeight();
return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));
};
$(window).scroll(function(){
if($('#boxorange').isOnScreen() == true){
$('#boxorange').css({"height":"400px"});
$('#boxorange').css({"width":"400px"});
} else{
$('#boxorange').css({"height":"200px"});
$('#boxorange').css({"width":"200px"}); }
});