ivanlucasgoncalves
10/2/2019 - 10:59 PM

Section in Viewport View

Simple Section Viewport for Scroll Event Animation.

// Scroll animation
function scrollEventAnimation() {
    features.forEach(feature => {
        if ($(`#section`).isInViewport()) {
            // do something
        } else {
            // do something else
        }
    });
}

$.fn.isInViewport = function() {
    var elementTop = $(this).offset().top;
    var elementBottom = elementTop + $(this).outerHeight();

    var viewportTop = $(window).scrollTop();
    var viewportBottom = viewportTop + $(window).height();

    return elementBottom > viewportTop && elementTop < viewportBottom;
};

$(window).on('resize scroll', scrollEventAnimation);