| 1 |
(function () { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
// Embed Block — lazy-load iframes on scroll for performance |
| 5 |
function initEmbeds() { |
| 6 |
var iframes = document.querySelectorAll('.bkbg-embed-iframe[loading="lazy"]'); |
| 7 |
if (!iframes.length) return; |
| 8 |
|
| 9 |
// Modern browsers handle loading="lazy" natively on iframes |
| 10 |
// This is a fallback for older browsers |
| 11 |
if ('loading' in HTMLIFrameElement.prototype) return; |
| 12 |
|
| 13 |
if (!('IntersectionObserver' in window)) return; |
| 14 |
|
| 15 |
var obs = new IntersectionObserver(function (entries) { |
| 16 |
entries.forEach(function (entry) { |
| 17 |
if (!entry.isIntersecting) return; |
| 18 |
var iframe = entry.target; |
| 19 |
var dataSrc = iframe.getAttribute('data-src'); |
| 20 |
if (dataSrc) { |
| 21 |
iframe.src = dataSrc; |
| 22 |
iframe.removeAttribute('data-src'); |
| 23 |
} |
| 24 |
obs.unobserve(iframe); |
| 25 |
}); |
| 26 |
}, { rootMargin: '200px 0px' }); |
| 27 |
|
| 28 |
iframes.forEach(function (iframe) { |
| 29 |
if (iframe.src && !iframe.getAttribute('data-src')) { |
| 30 |
iframe.setAttribute('data-src', iframe.src); |
| 31 |
iframe.removeAttribute('src'); |
| 32 |
} |
| 33 |
obs.observe(iframe); |
| 34 |
}); |
| 35 |
} |
| 36 |
|
| 37 |
if (document.readyState === 'loading') { |
| 38 |
document.addEventListener('DOMContentLoaded', initEmbeds); |
| 39 |
} else { |
| 40 |
initEmbeds(); |
| 41 |
} |
| 42 |
}()); |
| 43 |
|