Skip to content

Commit

Permalink
fix: isInView function for elements with zero width and height
Browse files Browse the repository at this point in the history
  • Loading branch information
janschoenherr committed Sep 7, 2018
1 parent a7f7466 commit de71b8d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,11 @@
# Changelog

## WIP

### Fixed

- Fix `isInView` function for elements with zero width and height

## 3.0.0 rc 14 (September 4, 2018)

### Fixed
Expand Down
37 changes: 20 additions & 17 deletions src/js/util/dimensions.js
@@ -1,7 +1,7 @@
import {css} from './style';
import {attr} from './attr';
import {isVisible} from './filter';
import {clamp, each, endsWith, includes, intersectRect, isDocument, isUndefined, isWindow, toFloat, toNode, ucfirst} from './lang';
import {clamp, each, endsWith, includes, intersectRect, isDocument, isUndefined, isWindow, pointInRect, toFloat, toNode, ucfirst} from './lang';

const dirs = {
width: ['x', 'left', 'right'],
Expand Down Expand Up @@ -297,38 +297,41 @@ export function isInView(element, topOffset = 0, leftOffset = 0, relativeToViewp
}

element = toNode(element);

const win = window(element);
let client, bounding;

if (relativeToViewport) {

return intersectRect(element.getBoundingClientRect(), {
client = element.getBoundingClientRect();
bounding = {
top: -topOffset,
left: -leftOffset,
bottom: topOffset + height(win),
right: leftOffset + width(win)
});
};

} else {

const [elTop, elLeft] = offsetPosition(element);
const {pageYOffset: top, pageXOffset: left} = win;

return intersectRect(
{
top: elTop,
left: elLeft,
bottom: elTop + element.offsetHeight,
right: elTop + element.offsetWidth
},
{
top: top - topOffset,
left: left - leftOffset,
bottom: top + topOffset + height(win),
right: left + leftOffset + width(win)
}
);
client = {
top: elTop,
left: elLeft,
bottom: elTop + element.offsetHeight,
right: elTop + element.offsetWidth
};
bounding = {
top: top - topOffset,
left: left - leftOffset,
bottom: top + topOffset + height(win),
right: left + leftOffset + width(win)
};
}

return intersectRect(client, bounding) || pointInRect({x: client.left, y: client.top}, bounding);

}

export function scrolledOver(element, heightOffset = 0) {
Expand Down

0 comments on commit de71b8d

Please sign in to comment.