Last active
December 20, 2015 21:59
-
-
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
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
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