fix iframe visibility detection (#1336)
This commit is contained in:
parent
cf0ecbfd4a
commit
fce049a911
|
@ -39,12 +39,13 @@
|
||||||
* and they may break if unexpected DOM stuff is present at `load` event
|
* and they may break if unexpected DOM stuff is present at `load` event
|
||||||
* so we'll add the styles only if the iframe becomes visible */
|
* so we'll add the styles only if the iframe becomes visible */
|
||||||
const {IntersectionObserver} = window;
|
const {IntersectionObserver} = window;
|
||||||
|
const xoEventId = `${Math.random()}`;
|
||||||
/** @type IntersectionObserver */
|
/** @type IntersectionObserver */
|
||||||
let xo;
|
let xo;
|
||||||
if (IntersectionObserver) {
|
if (IntersectionObserver) {
|
||||||
window[Symbol.for('xo')] = (el, cb) => {
|
window[Symbol.for('xo')] = (el, cb) => {
|
||||||
if (!xo) xo = new IntersectionObserver(onIntersect, {rootMargin: '100%'});
|
if (!xo) xo = new IntersectionObserver(onIntersect, {rootMargin: '100%'});
|
||||||
el.cb = cb;
|
el.addEventListener(xoEventId, cb, {once: true});
|
||||||
xo.observe(el);
|
xo.observe(el);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -235,7 +236,7 @@
|
||||||
for (const e of entries) {
|
for (const e of entries) {
|
||||||
if (e.isIntersecting) {
|
if (e.isIntersecting) {
|
||||||
xo.unobserve(e.target);
|
xo.unobserve(e.target);
|
||||||
tryCatch(e.target.cb);
|
e.target.dispatchEvent(new Event(xoEventId));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user