Skip to content

Instantly share code, notes, and snippets.

@Dexdot
Created May 14, 2018 13:49
Show Gist options
  • Save Dexdot/d18b7aab626b0e31058622c34a616c05 to your computer and use it in GitHub Desktop.
Save Dexdot/d18b7aab626b0e31058622c34a616c05 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