JSFiddle - React, Tailwind, and code Playground

by wilsonjonash

HTML

<link rel="stylesheet" href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css">
<div class="log">log</div>

scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
<div class="target label label-info">target element</div>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target element<br> scroll to target element<br>
scroll to target element<br> scroll to target element<br> scroll to target...

CSS

.log {
    position: fixed;
    right: 20px;
    top: 20px;
}

JavaScript

$.fn.is_on_screen = 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 isTargetVisble() {
    var retunVal = false;
    $('.target').each(function() {
        if( $(this).is_on_screen() ) {
            retunVal = true;
        }
    });
    return retunVal;
}

if( $('.target').length > 0 ) { // if target element exists in DOM
	if( isTargetVisble() ) { // if target element is visible on screen after DOM loaded
        $('.log').html('<div class="alert alert-success">target element is visible on screen</div>'); // log info		
	} else {
        $('.log').html('<div class="alert">target element is not visible on screen</div>'); // log info
	}
}
var _alreadyVisible = false;
$(window).scroll(function(){ // bind window scroll event
	if( $('.target').length > 0 ) { // if target element exists in DOM
		if( isTargetVisble() ) { // if target element is visible on screen after DOM loaded
			if(_alreadyVisible){
				//ignore the scroll
			} else {
				//the target just became visible
				_alreadyVisible = true;
				console.log('target became visible')
				$('.log').html('<div class="alert alert-success">target element is visible on screen</div>'); // log info
			}
		} else {
			if(_alreadyVisible){
				//the target was just hidden
				_alreadyVisible = false;
				$('.log').html('<div class="alert">target element is not visible on screen</div>'); // log info
				console.log('target became invisible')
			} else {
				//ignore
			}
		}
	}
});