-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
53 lines (47 loc) · 2.11 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
function redirectTo(page) {
window.location.href = page;
}
document.addEventListener('DOMContentLoaded', function() {
// URLs of the YouTube videos to preload
const videoUrls = [
'https://www.youtube.com/embed/qIazPStT87Y?si=gCwMgY2u3fCJ4EHA&start=80&end=624&autoplay=1&mute=1',
'https://www.youtube.com/embed/m6swATKSTog?si=_2BY1K6XaSo6NafQ&start=80&end=624&autoplay=1&mute=1',
'https://www.youtube.com/embed/UspEM0sTjQI?si=nhMKX-wyRilqImYQ&start=80&end=624&autoplay=1&mute=1',
'https://www.youtube.com/embed/djMTNx8xf5A?si=0v5M3IlUcjme3vCG&start=80&end=624&autoplay=1&mute=1'
];
// Create and load invisible iframes for preloading
videoUrls.forEach(url => {
const iframe = document.createElement('iframe');
iframe.src = url;
iframe.style.display = 'none'; // Hide iframe from view
iframe.setAttribute('loading', 'eager'); // Start loading immediately
document.body.appendChild(iframe); // Add iframe to the body
});
// Lazy-load remaining iframes when they enter the viewport
const lazyLoadIframes = () => {
const iframes = document.querySelectorAll('iframe');
if ('IntersectionObserver' in window) {
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const iframe = entry.target;
iframe.src = iframe.getAttribute('data-src');
observer.unobserve(iframe);
}
});
});
iframes.forEach(iframe => {
iframe.setAttribute('data-src', iframe.src);
iframe.src = ''; // Empty the src to delay loading
observer.observe(iframe);
});
} else {
// Fallback for browsers without IntersectionObserver support
iframes.forEach(iframe => {
iframe.src = iframe.getAttribute('data-src');
});
}
};
// Initialize lazy loading after preloading
lazyLoadIframes();
});