Created
April 13, 2016 12:01
-
-
Save vernondegoede/5b5638706aec5a80611010c88609d170 to your computer and use it in GitHub Desktop.
isElementInViewport: Get the lowest element in our viewport by a jQuery selector.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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'))})); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I got a reference error says: _ is not defined is that lodash or some variable you declared before ?