Is In Viewport Detection With IntersectionObserver API

This is a tiny example that shows how to detect if an element is in the viewport using the Intersection Observer API.

How to use it:

1.html

<div class="box">
    <div class="scroll">
        <div>
            <div class="target">Target</div>
        </div>
    </div>
    <span>In viewport: <strong>false</strong></span>
</div>

<pre><code class="language-js">function inViewport(elem, callback, options = {}) {
  return new IntersectionObserver(entries => {
    entries.forEach(entry => callback(entry));
  }, options).observe(document.querySelector(elem));
}

inViewport('.target', element => {
  //element.isIntersecting (bool) true/false
}, {
  root: document.querySelector('.scroll')
})</code></pre>

<a class="twitter" target="_blank" href="https://twitter.com/aaroniker_me"><svg xmlns="http://www.w3.org/2000/svg" width="72" height="72" viewBox="0 0 72 72"><path d="M67.812 16.141a26.246 26.246 0 0 1-7.519 2.06 13.134 13.134 0 0 0 5.756-7.244 26.127 26.127 0 0 1-8.313 3.176A13.075 13.075 0 0 0 48.182 10c-7.229 0-13.092 5.861-13.092 13.093 0 1.026.118 2.021.338 2.981-10.885-.548-20.528-5.757-26.987-13.679a13.048 13.048 0 0 0-1.771 6.581c0 4.542 2.312 8.551 5.824 10.898a13.048 13.048 0 0 1-5.93-1.638c-.002.055-.002.11-.002.162 0 6.345 4.513 11.638 10.504 12.84a13.177 13.177 0 0 1-3.449.457c-.846 0-1.667-.078-2.465-.231 1.667 5.2 6.499 8.986 12.23 9.09a26.276 26.276 0 0 1-16.26 5.606A26.21 26.21 0 0 1 4 55.976a37.036 37.036 0 0 0 20.067 5.882c24.083 0 37.251-19.949 37.251-37.249 0-.566-.014-1.134-.039-1.694a26.597 26.597 0 0 0 6.533-6.774z"></path></svg></a>

2.css

.box,
pre {
    width: 100%;
    max-width: 412px;
    border-radius: 8px;
}

.box {
    background: #fff;
    border: 2px solid #D1D6EE;
    position: relative;
    margin-bottom: 16px;
    .scroll {
        height: 200px;
        padding: 20px;
        overflow: auto;
        & > div {
            height: 300%;
            .target {
                margin-top: 100%;
                width: 80px;
                height: 80px;
                line-height: 80px;
                text-align: center;
                background: #275EFE;
                color: #fff;
                border-radius: 6px;
            }
        }
    }
    span {
        display: block;
        position: absolute;
        text-align: center;
        color: #646B8C;
        left: 0;
        right: 0;
        bottom: 0;
        font-size: 14px;
        padding: 8px 0;
        strong {
            font-weight: bold;
            color: #2B3044;
        }
    }
}

pre {
    background: #1A1E32;
    padding: 20px;
    code {
        font-family: 'Fira Mono';
        color: #BBC1E1;
        line-height: 1.4;
        font-size: 12px;
        .token {
            &.function {
                color: #5C86FF;
            }
            &.string {
                color: #CB7DD0;
            }
            &.parameter {
                color: #C4DDB2;
            }
            &.class-name {
                color: #E2C484;
            }
            &.keyword {
                color: #F9FFB1;
            }
            &.comment {
                opacity: .6;
            }
        }    
    }
}

body {
    font-family: 'Roboto', Arial;
    min-height: 100vh;
    background: #F6F8FF;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .twitter {
        position: fixed;
        display: block;
        right: 14px;
        bottom: 14px;
        svg {
            width: 32px;
            height: 32px;
            fill: #1da1f2;
        }
    }
}

html {
    box-sizing: border-box;
    -webkit-font-smoothing: antialiased;
}

* {
    box-sizing: inherit;
    &:before,
    &:after {
        box-sizing: inherit;
    }
}

3.Js

function inViewport(elem, callback, options = {}) {
    return new IntersectionObserver(entries => {
        entries.forEach(entry => callback(entry));
    }, options).observe(document.querySelector(elem));
}

inViewport('.target', element => {
    document.querySelector('.box span strong').textContent = element.isIntersecting;
}, {
    root: document.querySelector('.scroll')
});

//https://caniuse.com/#search=intersectionobserver

#javascript

Is In Viewport Detection With IntersectionObserver API
2.10 GEEK