-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Release 3.15
- Loading branch information
Showing
247 changed files
with
24,717 additions
and
14,433 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
function rocket_css_lazyload_launch() { | ||
|
||
const usable_pairs = typeof rocket_pairs === 'undefined' ? [] : rocket_pairs; | ||
|
||
|
||
const styleElement = document.querySelector('#wpr-lazyload-bg'); | ||
|
||
const threshold = rocket_lazyload_css_data.threshold || 300; | ||
|
||
const observer = new IntersectionObserver(entries => { | ||
entries.forEach(entry => { | ||
if (entry.isIntersecting) { | ||
const pairs = usable_pairs.filter(s => entry.target.matches(s.selector)); | ||
pairs.map(pair => { | ||
if (pair) { | ||
styleElement.innerHTML += pair.style; | ||
pair.elements.forEach(el => { | ||
el.setAttribute('data-rocket-lazy-bg', 'loaded'); | ||
// Stop observing the target element | ||
observer.unobserve(el); | ||
}); | ||
} | ||
}) | ||
} | ||
}); | ||
}, { | ||
rootMargin: threshold + 'px' | ||
}); | ||
|
||
function lazyload(e = []) { | ||
|
||
const pass = e.length === 0 || e.find(e => e.type !== 'attributes' || e.attributeName === 'class'); | ||
|
||
if(! pass ) { | ||
return; | ||
} | ||
|
||
usable_pairs.forEach(pair => { | ||
try { | ||
|
||
const elements = document.querySelectorAll(pair.selector); | ||
elements.forEach(el => { | ||
if(el.getAttribute('data-rocket-lazy-bg') === 'loaded') { | ||
return; | ||
} | ||
observer.observe(el); | ||
// Save el in the pair object (create a new empty array if it doesn't exist) | ||
(pair.elements ||= []).push(el); | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}); | ||
} | ||
|
||
lazyload(); | ||
|
||
const observe_DOM = (function(){ | ||
const MutationObserver = window.MutationObserver; | ||
|
||
return function( obj, callback ){ | ||
if( !obj || obj.nodeType !== 1 ) return; | ||
|
||
// define a new observer | ||
const mutationObserver = new MutationObserver(callback); | ||
|
||
// have the observer observe for changes in children | ||
mutationObserver.observe( obj, { attributes: true, childList:true, subtree:true }) | ||
return mutationObserver | ||
|
||
} | ||
|
||
})() | ||
|
||
const body = document.querySelector('body'); | ||
|
||
observe_DOM(body, lazyload) | ||
} | ||
|
||
rocket_css_lazyload_launch(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.