Skip to content

Instantly share code, notes, and snippets.

@alxscms
Created November 9, 2015 15:27
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 alxscms/fa2dc225c469c1037466 to your computer and use it in GitHub Desktop.
Save alxscms/fa2dc225c469c1037466 to your computer and use it in GitHub Desktop.
// true if display none or visibility hidden
function isVisible(el) {
var style = window.getComputedStyle(el);
return (style.display !== 'none')
}
// return true if element is in the window bounds or false if outside
function isInWindow(el) {
var viewportOffset = el.getBoundingClientRect();
// these are relative to the viewport
var top = viewportOffset.top;
var right = viewportOffset.right;
var bottom = viewportOffset.bottom;
var left = viewportOffset.left;
topInWindow = top > 0 && top < window.innerHeight;
bottomInWindow = bottom > 0 && bottom < window.innerHeight;
leftInWindow = left > 0 && left < window.innerWidth;
rightInWindow = right > 0 && right < window.innerWidth;
console.log("topInWindow", topInWindow);
console.log("bottomInWindow", bottomInWindow);
console.log("leftInWindow", leftInWindow);
console.log("rightInWindow", rightInWindow);
return (topInWindow || bottomInWindow) && (leftInWindow || rightInWindow);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment