1function hpuLazyLoad() { 2 // Exit early if IntersectionObserver is unsupported. 3 if ( ! ( 'IntersectionObserver' in window ) ) { 4 console.log( 'Lazy loading is disabled due to lack of browser support.' ); 5 return; 6 } 7 8 function maybeSwapAttribute( element, initial, updated ) { 9 // Exit early if the initial attribute is empty. 10 if ( null === element.getAttribute( initial ) ) { 11 return; 12 } 13 14 // Set the updated attribute to match the value of the initial attribute. 15 element.setAttribute( updated, element.getAttribute( initial ) ); 16 17 // Remove the initial attriburte. 18 element.removeAttribute( initial ); 19 } 20 21 // Select target elements. 22 const elements = document.querySelectorAll( 'iframe, img, source' ); 23 const excluded = [ 'skip-lazy', 'simple-parallax' ]; 24 const excludedContainers = [ 'foo-table' ]; 25 26 // Define the observer options. 27 const observerOptions = { 28 root: null, // default is viewport 29 rootMargin: '400px 0px' // intersection buffer (CSS style margin rule, MUST include `px` or `%`, even if 0) 30 }; 31 32 // Initialize the intersection observer. 33 const observer = new IntersectionObserver( ( entries ) => { 34 entries.forEach( entry => { 35 // Set the target element. 36 const element = entry.target; 37 38 if ( entry.isIntersecting ) { 39 // Manipulate intersecting element to trigger loading. 40 maybeSwapAttribute( element, 'data-src', 'src' ); 41 maybeSwapAttribute( element, 'data-srcset', 'srcset' ); 42 43 // Stop observing intersecting element. 44 observer.unobserve( element ); 45 } 46 else { 47 // Manipulate non-intersecting element to prevent early loading. 48 maybeSwapAttribute( element, 'src', 'data-src' ); 49 maybeSwapAttribute( element, 'srcset', 'data-srcset' ); 50 } 51 } ); 52 }, observerOptions ); 53 54 elements.forEach( element => { 55 // Exit early if the element classList contains any excluded classes. 56 if ( excluded.some( className => element.classList.contains( className ) ) ) { 57 return; 58 } 59 60 // Exit early if the element is inside an excluded container 61 if ( excludedContainers.some( className => element.closest( '.' + className ) ) ) { 62 return; 63 } 64 65 observer.observe( element ); 66 } ); 67} 68 69// Trigger lazy load on page load event. 70window.addEventListener( 'DOMContentLoaded', hpuLazyLoad );
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.