Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for items larger than viewport #110

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions addon/utils/is-in-viewport.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import Ember from 'ember';

const assign = Ember.assign || Ember.merge;

const defaultTolerance = {
top: 0,
left: 0,
bottom: 0,
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);
Expand All @@ -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));
}