JSFiddle - React, Tailwind, and code Playground

by Jon Fuller

HTML

<div class="space">This is to take up space<br/>This is to take up more space</div>
<div id="inViewport">Is this in the viewport?</div>

CSS

.space {
    background: red;
    height: 1000px;
}

JavaScript

$(document).ready(function(){
    $(window).scroll(function(){
        if ($('#inViewport').isOnScreen()) {
            // The element is visible, do something
            alert("in viewport!");
        } else {
            // The element is NOT visible, do something else
        }
    });
});

$.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));
    
};