Jquery is element in view scroll function

Checks if the element is in view and if it is it fires

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function isScrolledIntoView(el) {
    var elemTop = el.getBoundingClientRect().top;
    var elemBottom = el.getBoundingClientRect().bottom;
  
    var isVisible = (elemTop >= 0) && (elemBottom <= window.innerHeight);
    return isVisible;
}
 
$(window).scroll(function (event) {
    $( ".view_box" ).each(function( index ) {
        if (isScrolledIntoView(this)) {
            $(this).parent().addClass('in_view');
        }
    });    
});
Comments