Skip to content

Instantly share code, notes, and snippets.

@6ui11em
Created June 18, 2018 20:37
Show Gist options
  • Save 6ui11em/f8bb118db9cafe81013301a13d003292 to your computer and use it in GitHub Desktop.
Save 6ui11em/f8bb118db9cafe81013301a13d003292 to your computer and use it in GitHub Desktop.
Javascritp check if element is outside the viewport #javascript #js #ouside #viewport
/*!
* 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