cactimurray
8/21/2018 - 1:41 AM

Viewport Element Functions

Functions to check if items are in/out or centered in viewport.

function isCentreView($elem, buffer) {
    var docViewMid = $(window).scrollTop() + ($(window).height()/2);

    var elemTop = $elem.offset().top - (buffer * $(window).height());
    var elemBottom = elemTop + $elem.height() + (buffer * $(window).height());

    return ((docViewMid < elemBottom) && (docViewMid > elemTop));
};
function isScrolledIntoView($elem, bufferTop, bufferBottom) {
    var docViewTop = $(window).scrollTop() + (bufferBottom * $(window).height());
    var docViewBottom = $(window).scrollTop() + $(window).height() - (bufferTop * $(window).height());

    var elemTop = $elem.offset().top;
    var elemBottom = elemTop + $elem.height();

    if ((docViewTop < elemBottom) && (docViewBottom > elemTop)) {
        return 1;
    } else if (docViewTop >= elemBottom) {
        return 2;
    } else {
        return 0;
    }
};