Created
June 18, 2018 20:37
-
-
Save 6ui11em/f8bb118db9cafe81013301a13d003292 to your computer and use it in GitHub Desktop.
Javascritp check if element is outside the viewport #javascript #js #ouside #viewport
This file contains hidden or 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
/*! | |
* Check if an element is out of the viewport | |
* (c) 2018 Chris Ferdinandi, MIT License, https://gomakethings.com | |
* @param {Node} elem The element to check | |
* @return {Object} A set of booleans for each side of the element | |
*/ | |
var isOutOfViewport = function (elem) { | |
// Get element's bounding | |
var bounding = elem.getBoundingClientRect(); | |
// Check if it's out of the viewport on each side | |
var out = {}; | |
out.top = bounding.top < 0; | |
out.left = bounding.left < 0; | |
out.bottom = bounding.bottom > (window.innerHeight || document.documentElement.clientHeight); | |
out.right = bounding.right > (window.innerWidth || document.documentElement.clientWidth); | |
out.any = out.top || out.left || out.bottom || out.right; | |
out.all = out.top && out.left && out.bottom && out.right; | |
return out; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment