Skip to content

Commit e58bfee

Browse files
committed
feat: once attribute for intersectionObserver
1 parent e8f4c2d commit e58bfee

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

src/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ const CoCreateEvents = {
245245
? threshold.filter((value) => value >= 0 && value <= 1)
246246
: [0];
247247

248+
// Returns true if attribute is present AND does not equal "false"
249+
const onceAttr = el.getAttribute(`${prefix}-once`);
250+
const once = onceAttr !== null && onceAttr !== "false";
251+
248252
// Create IntersectionObserver
249253
const intersectionObserver = new IntersectionObserver(
250254
(entries) => {
@@ -274,6 +278,10 @@ const CoCreateEvents = {
274278
// entry.intersectionRatio
275279
// }
276280
);
281+
// Disconnect if once is true
282+
if (once) {
283+
intersectionObserver.unobserve(entry.target);
284+
}
277285
}
278286
});
279287
},

0 commit comments

Comments
 (0)