| | |
| | | { passive: true } |
| | | ); |
| | | |
| | | |
| | | window.previousBGSize = 'Small'; |
| | | window.bgSizes = { |
| | | Small: 500, |
| | | Med: 768, |
| | | Large: 1024 |
| | | }; |
| | | |
| | | window.bgObserver = new IntersectionObserver((entries) => { |
| | | entries.forEach(entry => { |
| | | if (entry.isIntersecting) { |
| | | let newSize = entry.target.dataset[`bg${window.previousBGSize}`]; |
| | | entry.target.style.backgroundImage = `url(${newSize})`; |
| | | entry.target.dataset.bgImg = window.previousBGSize; |
| | | window.bgObserver.unobserve(entry.target); |
| | | } |
| | | }) |
| | | }, |
| | | { |
| | | root: null, |
| | | rootMargin: '0px 0px -100px 0px', |
| | | threshold: 0 |
| | | }); |
| | | |
| | | function updateBG() { |
| | | let current = window.innerWidth; |
| | | let newWidth = getBGWidth(current); |
| | | |
| | | if (newWidth) { |
| | | window.previousBGSize = newWidth; |
| | | document.querySelectorAll('[data-bg-img]:not([data-bg-img="'+window.previousBGSize+'"])').forEach(img => { |
| | | window.bgObserver.observe(img); |
| | | }); |
| | | } |
| | | |
| | | } |
| | | function getBGWidth(width) { |
| | | let prev = window.previousBGSize; |
| | | |
| | | let check = { |
| | | Small: ['Med','Large'], |
| | | Med: ['Large'], |
| | | Large: false |
| | | }; |
| | | |
| | | if (!check[prev]) { |
| | | return false; |
| | | } |
| | | let next = 'Small'; |
| | | |
| | | check[prev].forEach(w => { |
| | | if (width => window.bgSizes[w]) { |
| | | next = w; |
| | | } |
| | | }); |
| | | return next; |
| | | } |
| | | |
| | | updateBG(); |
| | | |
| | | // Debounced resize to recalc scrollable height |
| | | window.addEventListener('resize', () => { |
| | | window.debouncer.schedule('recalc-max-scroll', () => { |
| | | updateMaxScroll(); |
| | | updateScrollProgress(window.scrollY || docEl.scrollTop || 0); |
| | | }, 20); |
| | | window.debouncer.schedule('bg-resize', () => { |
| | | updateBG(); |
| | | }); |
| | | }); |
| | | |
| | | // Initial setup |