"Check Is On Screen"
Bootstrap 4.1.1 Snippet by sdcarte

<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <!------ Include the above in your HEAD tag ----------> <div class="container"> <div class="row"> <div class="list-group"> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> <div class="list-group-item">Test</div> </div> </div> </div>
.list-group-item {transition: all 0.5s;} .notOnScreen {background-color: red;} .onScreen {background-color: green;}
$(document).ready(function() { $.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)); }; function addClassOnScreen(elem) { $(elem).each(function(){ var $this = $(this); if($this.isOnScreen()){ $this.addClass('onScreen'); $this.removeClass('notOnScreen'); } $(window).on('scroll', function () { if($this.isOnScreen()){ $this.addClass('onScreen'); $this.removeClass('notOnScreen'); }else { $this.removeClass('onScreen'); $this.addClass('notOnScreen'); } }); }); } addClassOnScreen('.list-group-item'); });

Related: See More


Questions / Comments: