Skip to content

Instantly share code, notes, and snippets.

@umkasanki
Created May 17, 2018 12:21
Show Gist options
  • Save umkasanki/4fdc261563b284f894c2a51655f54c16 to your computer and use it in GitHub Desktop.
Save umkasanki/4fdc261563b284f894c2a51655f54c16 to your computer and use it in GitHub Desktop.
Detecting if an element is in the Viewport : 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