fix iframe visibility detection (#1336)

This commit is contained in:
tophf 2021-09-24 09:43:10 +03:00 committed by GitHub
parent cf0ecbfd4a
commit fce049a911
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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));
} }
} }
} }