Skip to content

Instantly share code, notes, and snippets.

@neodigm
Last active September 27, 2018 19:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neodigm/1673a1881c2276c49e0dbe8b76af0bf6 to your computer and use it in GitHub Desktop.
Save neodigm/1673a1881c2276c49e0dbe8b76af0bf6 to your computer and use it in GitHub Desktop.
JavaScript Lazy Load
"use strict";
var fLazy = (function(doc){
return { "init" : function( _bMobl ){
var _aE = [].slice.call( doc.querySelectorAll( "[data-lz-src]" ) );
_aE.filter( function( _e ){
if( _bMobl ){ // isMobile
if( _e.dataset.lzM ){ // data-lz-m
_e.src = _e.dataset.lzSrc;
}
}else{
_e.src = _e.dataset.lzSrc;
}
});
}
}
})( document );
// You then call fLazy.init(), on intersection, setTimeOut or first Click.
@neodigm
Copy link
Author

neodigm commented Apr 16, 2018

I am sharing a simple lazy loader that I wrote in JS. This works but at the time of this writing Google Lighthouse was pretty buggy in auditing lazy loads, so be patient.

@neodigm
Copy link
Author

neodigm commented Sep 27, 2018

I've updated so that it now can process a sub-set of images based on a isMobile switch. Also, I believe that the Lighthouse extension v3.2 is a bit more stable, so try that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment