eduzambl
9/8/2015 - 5:00 AM

Check if element is on viewport

Check if element is on viewport

#How to use

inViewport(element) it will return true if it is or false if not on viewport.
you can use it on window scroll

$(window).on('scroll', function() { ... });
function inViewport(element) {
  var rect = element.getBoundingClientRect();
  var html = document.documentElement;
  return (
    rect.top >= 0 &&
    rect.left >= 0 &&
    rect.bottom <= (window.innerHeight || html.clientHeight) &&
    rect.right <= (window.innerWidth || html.clientWidth)
  );
}