Skip to content

Commit ad1dea1

Browse files
author
Daniel Schmidt
committed
Add back isIntersecting, but merge usage with ratio. Fixes #8
1 parent 8564848 commit ad1dea1

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/intersection.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,21 @@ export function unobserve(element) {
6464
function onChange(changes) {
6565
changes.forEach(intersection => {
6666
if (INSTANCE_MAP.has(intersection.target)) {
67-
const { callback, visible, threshold } = INSTANCE_MAP.get(
68-
intersection.target,
69-
)
70-
// Trigger on 0 ratio only when not visible.
71-
const inView = visible
72-
? intersection.intersectionRatio > threshold
73-
: intersection.intersectionRatio >= threshold
67+
const { isIntersecting, intersectionRatio, target } = intersection
68+
const { callback, visible, threshold } = INSTANCE_MAP.get(target)
7469

75-
INSTANCE_MAP.set(intersection.target, {
70+
// Trigger on 0 ratio only when not visible. This is fallback for browsers without isIntersecting support
71+
let inView = visible
72+
? intersectionRatio > threshold
73+
: intersectionRatio >= threshold
74+
75+
if (isIntersecting !== undefined) {
76+
// If isIntersecting is defined, ensure that the element is actually intersecting.
77+
// Otherwise it reports a threshold of 0
78+
inView = inView && isIntersecting
79+
}
80+
81+
INSTANCE_MAP.set(target, {
7682
callback,
7783
visible: inView,
7884
threshold,

0 commit comments

Comments
 (0)