Skip to content

Instantly share code, notes, and snippets.

@emersonbroga
Created February 27, 2017 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emersonbroga/619084f79f21a0e97bbc343fb6c52cdf to your computer and use it in GitHub Desktop.
Save emersonbroga/619084f79f21a0e97bbc343fb6c52cdf to your computer and use it in GitHub Desktop.
Check if element is visible (in view)
function Utils() {}
Utils.prototype = {
constructor: Utils,
isElementInView: function (element, fullyInView) {
var pageTop = $(window).scrollTop();
var pageBottom = pageTop + $(window).height();
var elementTop = $(element).offset().top;
var elementBottom = elementTop + $(element).height();
if (fullyInView === true) {
return ((pageTop < elementTop) && (pageBottom > elementBottom));
} else {
return ((elementTop <= pageBottom) && (elementBottom >= pageTop));
}
}
};
var Utils = new Utils();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment