Skip to content

Instantly share code, notes, and snippets.

@gartes
Forked from Dexdot/jq.viewport.js
Created February 7, 2022 10:26
Show Gist options
  • Save gartes/53a71b84787d4b253918f0320507a8b9 to your computer and use it in GitHub Desktop.
Save gartes/53a71b84787d4b253918f0320507a8b9 to your computer and use it in GitHub Desktop.
jQuery viewport checker
$.fn.isInViewport = function() {
const elementTop = $(this).offset().top;
const elementBottom = elementTop + $(this).outerHeight();
const viewportTop = $(window).scrollTop();
const 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