Skip to content

Instantly share code, notes, and snippets.

@vernondegoede
Created April 13, 2016 12:01
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 vernondegoede/5b5638706aec5a80611010c88609d170 to your computer and use it in GitHub Desktop.
Save vernondegoede/5b5638706aec5a80611010c88609d170 to your computer and use it in GitHub Desktop.
isElementInViewport: Get the lowest element in our viewport by a jQuery selector.
/**
* Pass in jQuery selector, for example:
*
* var elements = $('.my_element');
* console.log(isElementInViewport(elements))
*/
function isElementInViewport(elements) {
var visibleElements = [];
elements.each(function () {
var $el = $(this),
rect = $el[0].getBoundingClientRect(),
isVisible = (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
if (isVisible) {
visibleElements.push({
el: $el,
bottom: rect.bottom
});
}
});
return _.first(_.where(visibleElements, {bottom: _.max(_.pluck(visibleElements, 'bottom'))}));
}
@Hostname47
Copy link

I got a reference error says: _ is not defined is that lodash or some variable you declared before ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment