mirror of
https://github.com/StarCitizenTools/mediawiki-skins-Citizen.git
synced 2024-11-15 10:38:19 +00:00
refactor: convert skin.citizen.scripts.lazyload to ES6
This commit is contained in:
parent
98ee513e39
commit
43072b1947
|
@ -1,13 +1,13 @@
|
||||||
/*
|
/*
|
||||||
* Citizen - Lazyload JS
|
|
||||||
* https://starcitizen.tools
|
|
||||||
*
|
|
||||||
* Lazyloading images with Native API or IntersectionObserver
|
* Lazyloading images with Native API or IntersectionObserver
|
||||||
|
* TODO: Remove the features since it is depreciated by $wgNativeImageLazyLoading in 1.35
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var observer;
|
/**
|
||||||
|
* @param {document} document
|
||||||
function main() {
|
* @return {void}
|
||||||
|
*/
|
||||||
|
function main( document ) {
|
||||||
// Native API
|
// Native API
|
||||||
if ( 'loading' in HTMLImageElement.prototype ) {
|
if ( 'loading' in HTMLImageElement.prototype ) {
|
||||||
document.querySelectorAll( 'img.lazy' ).forEach( function ( img ) {
|
document.querySelectorAll( 'img.lazy' ).forEach( function ( img ) {
|
||||||
|
@ -24,12 +24,12 @@ function main() {
|
||||||
} else {
|
} else {
|
||||||
// IntersectionObserver API
|
// IntersectionObserver API
|
||||||
if ( typeof IntersectionObserver !== 'undefined' && 'forEach' in NodeList.prototype ) {
|
if ( typeof IntersectionObserver !== 'undefined' && 'forEach' in NodeList.prototype ) {
|
||||||
observer = new IntersectionObserver( function ( changes ) {
|
const observer = new IntersectionObserver( ( changes ) => {
|
||||||
if ( 'connection' in navigator && navigator.connection.saveData === true ) {
|
if ( 'connection' in navigator && navigator.connection.saveData === true ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
changes.forEach( function ( change ) {
|
changes.forEach( ( change ) => {
|
||||||
if ( change.isIntersecting ) {
|
if ( change.isIntersecting ) {
|
||||||
change.target.setAttribute( 'src', change.target.getAttribute( 'data-src' ) );
|
change.target.setAttribute( 'src', change.target.getAttribute( 'data-src' ) );
|
||||||
change.target.removeAttribute( 'data-src' );
|
change.target.removeAttribute( 'data-src' );
|
||||||
|
@ -51,4 +51,4 @@ function main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
main();
|
main( document );
|
||||||
|
|
Loading…
Reference in a new issue