/* Get the TOP position of a given element. */function getPositionTop(element){var offset = 0;while(element) {offset += element["offsetTop"];element = element.offsetParent;}return offset;}
/* Is a given element is visible or not? */function isElementVisible(eltId) {var elt = document.getElementById(eltId);if (!elt) {// Element not found.return false;}// Get the top and bottom position of the given element.var posTop = getPositionTop(elt);var posBottom = posTop + elt.offsetHeight;// Get the top and bottom position of the *visible* part of the window.var visibleTop = document.body.scrollTop;var visibleBottom = visibleTop + document.documentElement.offsetHeight;return ((posBottom >= visibleTop) && (posTop <= visibleBottom));}
var isElementInView = Utils.isElementInView($('#flyout-left-container'), false);
if (isElementInView) {console.log('in view');} else {console.log('out of view');}
var visibleY = function(el){var rect = el.getBoundingClientRect(), top = rect.top, height = rect.height,el = el.parentNode// Check if bottom of the element is off the pageif (rect.bottom < 0) return false// Check its within the document viewportif (top > document.documentElement.clientHeight) return falsedo {rect = el.getBoundingClientRect()if (top <= rect.bottom === false) return false// Check if the element is out of view due to a container scrollingif ((top + height) <= rect.top) return falseel = el.parentNode} while (el != document.body)return true};
<script type="text/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));};
if( $('.target').length > 0 ) { // if target element exists in DOMif( $('.target').is_on_screen() ) { // 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}}$(window).on('scroll', function(){ // bind window scroll eventif( $('.target').length > 0 ) { // if target element exists in DOMif( $('.target').is_on_screen() ) { // 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}}});</script>
$(".my-elem:inview"); //returns only element that is in view$(".my-elem").is(":inview"); //check if element is in view$(".my-elem:inview").length; //check how many elements are in view
$(document).scroll(function() {if($("#div2").is(':onScreen')) {console.log("Element appeared on Screen");//do all your stuffs here when element is visible.}else {console.log("Element not on Screen");//do all your stuffs here when element is not visible.}});
/*** Checks if element is on the screen (Y axis only), returning true* even if the element is only partially on screen.** @param element* @returns {boolean}*/function isOnScreenY(element) {var screen_top_position = window.scrollY;var screen_bottom_position = screen_top_position + window.innerHeight;
var element_top_position = element.offsetTop;var element_bottom_position = element_top_position + element.offsetHeight;
return (inRange(element_top_position, screen_top_position, screen_bottom_position)|| inRange(element_bottom_position, screen_top_position, screen_bottom_position));}
/*** Checks if x is in range (in-between) the* value of a and b (in that order). Also returns true* if equal to either value.** @param x* @param a* @param b* @returns {boolean}*/function inRange(x, a, b) {return (x >= a && x <= b);}
// define an observer instancevar observer = new IntersectionObserver(onIntersection, {root: null, // default is the viewportthreshold: .5 // percentage of target's visible area. Triggers "onIntersection"})
// callback is called on intersection changefunction onIntersection(entries, opts){entries.forEach(entry =>entry.target.classList.toggle('visible', entry.isIntersecting))}
// Use the observer to observe an elementobserver.observe( document.querySelector('.box') )
// To stop observing:// observer.unobserve(entry.target)
/*** Is element within visible region of a scrollable container* @param {HTMLElement} el - element to test* @returns {boolean} true if within visible region, otherwise false*/function isScrolledIntoView(el) {var rect = el.getBoundingClientRect();return (rect.top >= 0) && (rect.bottom <= window.innerHeight);}
var observer = new IntersectionObserver(function(entries) {if(entries[0].isIntersecting === true)console.log('Element has just become visible in screen');}, { threshold: [0] });
observer.observe(document.querySelector("#main-container"));