Skip to content

Commit

Permalink
Improvements to horizontal viewport check
Browse files Browse the repository at this point in the history
* Right was calculated incorrectly (but was never used), which has been fixed.
* The horizontal viewport check took the absolute value of the element's left,
  which didn't accurately check the viewport left/element right bound. This has
  been modified to check the element's left against the viewport's right
  (whether the element is within the right side of the viewport) and the
  element's right against the viewport's left (whether the element is within
  the left side of the viewport).
  • Loading branch information
npafundi committed Mar 24, 2015
1 parent 554462d commit 381242d
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/isInViewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down

0 comments on commit 381242d

Please sign in to comment.