From dcc6f1a19035759cc39b3e445d16b94bd3b1963a Mon Sep 17 00:00:00 2001 From: Rik Hengeveld Date: Tue, 12 Apr 2016 17:47:43 +0200 Subject: [PATCH 1/3] Fix for items larger than viewport For each axis (X,Y) the item is considered in the viewport when: - It's dimensions are fully within the viewport (original calculation) - It's dimension are fully outside the viewport (new) So the behaviour only changes for items that are actually taller/wider :) This probably also fixes https://github.com/DockYard/ember-in-viewport/issues/37 --- addon/utils/is-in-viewport.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/addon/utils/is-in-viewport.js b/addon/utils/is-in-viewport.js index 5198bad6..14a27512 100644 --- a/addon/utils/is-in-viewport.js +++ b/addon/utils/is-in-viewport.js @@ -9,6 +9,14 @@ 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; + return exceedingLimit + ? start <= startTolerance && (end - endTolerance) >= limit + : (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 +27,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)); } From 96995f1af71a40b6cf9d73b40ede237af1a3bad1 Mon Sep 17 00:00:00 2001 From: Lauren Tan Date: Sat, 20 Aug 2016 11:03:14 -0700 Subject: [PATCH 2/3] Fix formatting --- addon/utils/is-in-viewport.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/addon/utils/is-in-viewport.js b/addon/utils/is-in-viewport.js index 14a27512..10e26946 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, @@ -12,9 +11,12 @@ const defaultTolerance = { 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; - return exceedingLimit - ? start <= startTolerance && (end - endTolerance) >= limit - : (start + startTolerance) >= 0 && (end - endTolerance) <= limit; + + if (exceedingLimit) { + return start <= startTolerance && (end - endTolerance) >= limit; + } + + return (start + startTolerance) >= 0 && (end - endTolerance) <= limit; }; export default function isInViewport(boundingClientRect = {}, height = 0, width = 0, tolerance = defaultTolerance) { @@ -27,6 +29,6 @@ export default function isInViewport(boundingClientRect = {}, height = 0, width right: rightTolerance } = tolerances; - return isAxisInViewport(top, topTolerance, (Math.round(bottom), bottomTolerance, Math.round(height)) && - isAxisInViewport(left, leftTolerance, 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)); } From d7bcc0131a58d9657ecfcc685f571eba82ada574 Mon Sep 17 00:00:00 2001 From: Rob Chandler Date: Tue, 7 Nov 2017 11:56:22 +0000 Subject: [PATCH 3/3] Fixes end calculation when element exceeds viewport height --- addon/utils/is-in-viewport.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addon/utils/is-in-viewport.js b/addon/utils/is-in-viewport.js index 10e26946..82872ea4 100644 --- a/addon/utils/is-in-viewport.js +++ b/addon/utils/is-in-viewport.js @@ -13,7 +13,7 @@ const isAxisInViewport = function(start, startTolerance, end, endTolerance, limi const exceedingLimit = (end + endTolerance) - (start + startTolerance) > limit; if (exceedingLimit) { - return start <= startTolerance && (end - endTolerance) >= limit; + return start <= startTolerance && end - limit >= start; } return (start + startTolerance) >= 0 && (end - endTolerance) <= limit;