I can't seem to figure out why this code as a while loop and as a recursive function is running infinitely. I am checking if an element is in the viewport, if it is not, then I move its parent's top offset up until its child is out of the viewport.
HTML structure:
<div id="outter">
<div id="inner"></div>
<div id="inner"></div>
<div id="inner"></div>
</div>
They all have set heights and widths and are display:inline-block and visible.
I've already have a jQuery psuedoclass that checks if an element is offscreen, as `.is(':offscreen'). It works fine, but I'm wondering if the original value is caching somewhere in jQuery.
Here in the code:
var top=1;
do{
$('#outter').css('top', top);
top++;
}
while(!$('#inner').is(':offscreen'));
Here is the code for the offscreen psuedoclass, found on StackOverflow:
jQuery.expr.filters.offscreen = function(el) {
var rect = el.getBoundingClientRect();
return(
(rect.x + rect.width) < 0 || (rect.y + rect.height) < 0 ||
(rect.x > window.innerWidth || rect.y > window.innerHeight));
};