Skip to content

Instantly share code, notes, and snippets.

@jpneey
Created April 7, 2021 22:39
Show Gist options
  • Save jpneey/356a46e15a940a8f57c99949f3cc3c95 to your computer and use it in GitHub Desktop.
Save jpneey/356a46e15a940a8f57c99949f3cc3c95 to your computer and use it in GitHub Desktop.
Detect element if is in view port - jquery
$.fn.isInViewport = function() {
var elementTop = $(this).offset().top;
var elementBottom = elementTop + $(this).outerHeight();
var viewportTop = $(window).scrollTop();
var viewportBottom = viewportTop + $(window).height();
return elementBottom > viewportTop && elementTop < viewportBottom;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment