isInViewport function

bool isInViewport(
  1. Element element, {
  2. bool fully = false,
})

Returns true if element is visible in viewport.

Implementation

bool isInViewport(Element element, {bool fully = false}) {
  var rect = element.getBoundingClientRect();

  var windowWidth = min(
    window.innerWidth,
    document.documentElement!.clientWidth,
  );
  var windowHeight = min(
    window.innerHeight,
    document.documentElement!.clientHeight,
  );

  if (fully) {
    return rect.left >= 0 &&
        rect.top >= 0 &&
        rect.right < windowWidth &&
        rect.bottom < windowHeight;
  } else {
    return rect.bottom > 0 &&
        rect.right > 0 &&
        rect.left < windowWidth &&
        rect.top < windowHeight;
  }
}