diff --git a/addon/utils/is-in-viewport.js b/addon/utils/is-in-viewport.js index 5198bad6..82872ea4 100644 --- a/addon/utils/is-in-viewport.js +++ b/addon/utils/is-in-viewport.js @@ -1,7 +1,6 @@ import Ember from 'ember'; const assign = Ember.assign || Ember.merge; - const defaultTolerance = { top: 0, left: 0, @@ -9,6 +8,17 @@ const defaultTolerance = { right: 0 }; +const isAxisInViewport = function(start, startTolerance, end, endTolerance, limit) { + // Dimensions are fully LARGER than the viewport or fully WITHIN the viewport. + const exceedingLimit = (end + endTolerance) - (start + startTolerance) > limit; + + if (exceedingLimit) { + return start <= startTolerance && end - limit >= start; + } + + return (start + startTolerance) >= 0 && (end - endTolerance) <= limit; +}; + export default function isInViewport(boundingClientRect = {}, height = 0, width = 0, tolerance = defaultTolerance) { const { top, left, bottom, right } = boundingClientRect; const tolerances = assign(assign({}, defaultTolerance), tolerance); @@ -19,10 +29,6 @@ export default function isInViewport(boundingClientRect = {}, height = 0, width right: rightTolerance } = tolerances; - return ( - (top + topTolerance) >= 0 && - (left + leftTolerance) >= 0 && - (Math.round(bottom) - bottomTolerance) <= Math.round(height) && - (Math.round(right) - rightTolerance) <= Math.round(width) - ); + return isAxisInViewport(top, topTolerance, Math.round(bottom), bottomTolerance, Math.round(height)) && + isAxisInViewport(left, leftTolerance, Math.round(right), rightTolerance, Math.round(width)); }