Skip to content

Instantly share code, notes, and snippets.

@ianpgall
Last active December 20, 2015 21:59
Show Gist options
  • Save ianpgall/6201650 to your computer and use it in GitHub Desktop.
Save ianpgall/6201650 to your computer and use it in GitHub Desktop.
JavaScript function that determines if an element is in the window's viewport
var IsInViewport = (function () {
"use strict";
var func;
func = function (el, par) {
var ret, elRect, parRect;
elRect = el.getBoundingClientRect();
if (par) {
parRect = par.getBoundingClientRect();
} else {
parRect = {
top: 0,
left: 0,
bottom: (window.innerHeight || document. documentElement.clientHeight),
right: (window.innerWidth || document. documentElement.clientWidth)
};
}
ret = (
elRect.top >= parRect.top &&
elRect.left >= parRect.left &&
elRect.bottom <= parRect.bottom &&
elRect.right <= parRect.right
);
return ret;
};
return func;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment