Skip to content

Instantly share code, notes, and snippets.

@psymeon
Created September 3, 2014 12:40
Show Gist options
  • Save psymeon/89c25019a64397c51f71 to your computer and use it in GitHub Desktop.
Save psymeon/89c25019a64397c51f71 to your computer and use it in GitHub Desktop.
Simple jQuery function that determines whether DOM element is in the browser's viewport.
// Custom jQuery function.
// Determines if element is withing the viewport.
$.fn.inViewport = function() {
$el = this;
var elTop = $el.offset().top;
var elBot = elTop + $el.outerHeight(true);
var winScrollTop = $(window).scrollTop();
var winHeight = $(window).height();
var inViewport = winScrollTop <= elBot && (winScrollTop + winHeight) >= elTop;
return inViewport;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment