diff --git a/lib/isInViewport.js b/lib/isInViewport.js index 9868df6..6bcc1aa 100644 --- a/lib/isInViewport.js +++ b/lib/isInViewport.js @@ -90,10 +90,10 @@ var viewportRect = $viewport.get(0).getBoundingClientRect(); //recalculate these relative to viewport - top = top - viewportRect.top; + top = top - viewportRect.top; bottom = bottom - viewportRect.top; - left = left - viewportRect.left; - right = left + $viewportWidth; + left = left - viewportRect.left; + right = right - viewportRect.left; //get the scrollbar width from cache or calculate it isInViewport.scrollBarWidth = isInViewport.scrollBarWidth || getScrollbarWidth($viewport); @@ -113,10 +113,12 @@ //the element is NOT in viewport iff it is completely out of //viewport laterally or if it is completely out of the tolerance //region. Therefore, if it is partially in view then it is considered - //to be in the viewport and hence true is returned + //to be in the viewport and hence true is returned. Because we have adjusted + //the left/right positions relative to the viewport, we should check the + //element's right against the viewport's 0 (left side), and the element's + //left against the viewport's width to see if it is outside of the viewport. - //if the element is laterally outside the viewport - if (Math.abs(left) >= $viewportWidth) + if (right <= 0 || left >= $viewportWidth) return isVisibleFlag; //if the element is bound to some tolerance